lpddr_cmds.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * LPDDR flash memory device operations. This module provides read, write,
  3. * erase, lock/unlock support for LPDDR flash memories
  4. * (C) 2008 Korolev Alexey <akorolev@infradead.org>
  5. * (C) 2008 Vasiliy Leonenko <vasiliy.leonenko@gmail.com>
  6. * Many thanks to Roman Borisov for initial enabling
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. * TODO:
  23. * Implement VPP management
  24. * Implement XIP support
  25. * Implement OTP support
  26. */
  27. #include <linux/mtd/pfow.h>
  28. #include <linux/mtd/qinfo.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
  32. size_t *retlen, u_char *buf);
  33. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to,
  34. size_t len, size_t *retlen, const u_char *buf);
  35. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  36. unsigned long count, loff_t to, size_t *retlen);
  37. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr);
  38. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  39. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  40. static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
  41. size_t *retlen, void **mtdbuf, resource_size_t *phys);
  42. static int lpddr_unpoint(struct mtd_info *mtd, loff_t adr, size_t len);
  43. static int get_chip(struct map_info *map, struct flchip *chip, int mode);
  44. static int chip_ready(struct map_info *map, struct flchip *chip, int mode);
  45. static void put_chip(struct map_info *map, struct flchip *chip);
  46. struct mtd_info *lpddr_cmdset(struct map_info *map)
  47. {
  48. struct lpddr_private *lpddr = map->fldrv_priv;
  49. struct flchip_shared *shared;
  50. struct flchip *chip;
  51. struct mtd_info *mtd;
  52. int numchips;
  53. int i, j;
  54. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  55. if (!mtd) {
  56. printk(KERN_ERR "Failed to allocate memory for MTD device\n");
  57. return NULL;
  58. }
  59. mtd->priv = map;
  60. mtd->type = MTD_NORFLASH;
  61. /* Fill in the default mtd operations */
  62. mtd->_read = lpddr_read;
  63. mtd->type = MTD_NORFLASH;
  64. mtd->flags = MTD_CAP_NORFLASH;
  65. mtd->flags &= ~MTD_BIT_WRITEABLE;
  66. mtd->_erase = lpddr_erase;
  67. mtd->_write = lpddr_write_buffers;
  68. mtd->_writev = lpddr_writev;
  69. mtd->_lock = lpddr_lock;
  70. mtd->_unlock = lpddr_unlock;
  71. if (map_is_linear(map)) {
  72. mtd->_point = lpddr_point;
  73. mtd->_unpoint = lpddr_unpoint;
  74. }
  75. mtd->size = 1 << lpddr->qinfo->DevSizeShift;
  76. mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
  77. mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
  78. shared = kmalloc(sizeof(struct flchip_shared) * lpddr->numchips,
  79. GFP_KERNEL);
  80. if (!shared) {
  81. kfree(lpddr);
  82. kfree(mtd);
  83. return NULL;
  84. }
  85. chip = &lpddr->chips[0];
  86. numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
  87. for (i = 0; i < numchips; i++) {
  88. shared[i].writing = shared[i].erasing = NULL;
  89. mutex_init(&shared[i].lock);
  90. for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
  91. *chip = lpddr->chips[i];
  92. chip->start += j << lpddr->chipshift;
  93. chip->oldstate = chip->state = FL_READY;
  94. chip->priv = &shared[i];
  95. /* those should be reset too since
  96. they create memory references. */
  97. init_waitqueue_head(&chip->wq);
  98. mutex_init(&chip->mutex);
  99. chip++;
  100. }
  101. }
  102. return mtd;
  103. }
  104. EXPORT_SYMBOL(lpddr_cmdset);
  105. static int wait_for_ready(struct map_info *map, struct flchip *chip,
  106. unsigned int chip_op_time)
  107. {
  108. unsigned int timeo, reset_timeo, sleep_time;
  109. unsigned int dsr;
  110. flstate_t chip_state = chip->state;
  111. int ret = 0;
  112. /* set our timeout to 8 times the expected delay */
  113. timeo = chip_op_time * 8;
  114. if (!timeo)
  115. timeo = 500000;
  116. reset_timeo = timeo;
  117. sleep_time = chip_op_time / 2;
  118. for (;;) {
  119. dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
  120. if (dsr & DSR_READY_STATUS)
  121. break;
  122. if (!timeo) {
  123. printk(KERN_ERR "%s: Flash timeout error state %d \n",
  124. map->name, chip_state);
  125. ret = -ETIME;
  126. break;
  127. }
  128. /* OK Still waiting. Drop the lock, wait a while and retry. */
  129. mutex_unlock(&chip->mutex);
  130. if (sleep_time >= 1000000/HZ) {
  131. /*
  132. * Half of the normal delay still remaining
  133. * can be performed with a sleeping delay instead
  134. * of busy waiting.
  135. */
  136. msleep(sleep_time/1000);
  137. timeo -= sleep_time;
  138. sleep_time = 1000000/HZ;
  139. } else {
  140. udelay(1);
  141. cond_resched();
  142. timeo--;
  143. }
  144. mutex_lock(&chip->mutex);
  145. while (chip->state != chip_state) {
  146. /* Someone's suspended the operation: sleep */
  147. DECLARE_WAITQUEUE(wait, current);
  148. set_current_state(TASK_UNINTERRUPTIBLE);
  149. add_wait_queue(&chip->wq, &wait);
  150. mutex_unlock(&chip->mutex);
  151. schedule();
  152. remove_wait_queue(&chip->wq, &wait);
  153. mutex_lock(&chip->mutex);
  154. }
  155. if (chip->erase_suspended || chip->write_suspended) {
  156. /* Suspend has occurred while sleep: reset timeout */
  157. timeo = reset_timeo;
  158. chip->erase_suspended = chip->write_suspended = 0;
  159. }
  160. }
  161. /* check status for errors */
  162. if (dsr & DSR_ERR) {
  163. /* Clear DSR*/
  164. map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
  165. printk(KERN_WARNING"%s: Bad status on wait: 0x%x \n",
  166. map->name, dsr);
  167. print_drs_error(dsr);
  168. ret = -EIO;
  169. }
  170. chip->state = FL_READY;
  171. return ret;
  172. }
  173. static int get_chip(struct map_info *map, struct flchip *chip, int mode)
  174. {
  175. int ret;
  176. DECLARE_WAITQUEUE(wait, current);
  177. retry:
  178. if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
  179. && chip->state != FL_SYNCING) {
  180. /*
  181. * OK. We have possibility for contension on the write/erase
  182. * operations which are global to the real chip and not per
  183. * partition. So let's fight it over in the partition which
  184. * currently has authority on the operation.
  185. *
  186. * The rules are as follows:
  187. *
  188. * - any write operation must own shared->writing.
  189. *
  190. * - any erase operation must own _both_ shared->writing and
  191. * shared->erasing.
  192. *
  193. * - contension arbitration is handled in the owner's context.
  194. *
  195. * The 'shared' struct can be read and/or written only when
  196. * its lock is taken.
  197. */
  198. struct flchip_shared *shared = chip->priv;
  199. struct flchip *contender;
  200. mutex_lock(&shared->lock);
  201. contender = shared->writing;
  202. if (contender && contender != chip) {
  203. /*
  204. * The engine to perform desired operation on this
  205. * partition is already in use by someone else.
  206. * Let's fight over it in the context of the chip
  207. * currently using it. If it is possible to suspend,
  208. * that other partition will do just that, otherwise
  209. * it'll happily send us to sleep. In any case, when
  210. * get_chip returns success we're clear to go ahead.
  211. */
  212. ret = mutex_trylock(&contender->mutex);
  213. mutex_unlock(&shared->lock);
  214. if (!ret)
  215. goto retry;
  216. mutex_unlock(&chip->mutex);
  217. ret = chip_ready(map, contender, mode);
  218. mutex_lock(&chip->mutex);
  219. if (ret == -EAGAIN) {
  220. mutex_unlock(&contender->mutex);
  221. goto retry;
  222. }
  223. if (ret) {
  224. mutex_unlock(&contender->mutex);
  225. return ret;
  226. }
  227. mutex_lock(&shared->lock);
  228. /* We should not own chip if it is already in FL_SYNCING
  229. * state. Put contender and retry. */
  230. if (chip->state == FL_SYNCING) {
  231. put_chip(map, contender);
  232. mutex_unlock(&contender->mutex);
  233. goto retry;
  234. }
  235. mutex_unlock(&contender->mutex);
  236. }
  237. /* Check if we have suspended erase on this chip.
  238. Must sleep in such a case. */
  239. if (mode == FL_ERASING && shared->erasing
  240. && shared->erasing->oldstate == FL_ERASING) {
  241. mutex_unlock(&shared->lock);
  242. set_current_state(TASK_UNINTERRUPTIBLE);
  243. add_wait_queue(&chip->wq, &wait);
  244. mutex_unlock(&chip->mutex);
  245. schedule();
  246. remove_wait_queue(&chip->wq, &wait);
  247. mutex_lock(&chip->mutex);
  248. goto retry;
  249. }
  250. /* We now own it */
  251. shared->writing = chip;
  252. if (mode == FL_ERASING)
  253. shared->erasing = chip;
  254. mutex_unlock(&shared->lock);
  255. }
  256. ret = chip_ready(map, chip, mode);
  257. if (ret == -EAGAIN)
  258. goto retry;
  259. return ret;
  260. }
  261. static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
  262. {
  263. struct lpddr_private *lpddr = map->fldrv_priv;
  264. int ret = 0;
  265. DECLARE_WAITQUEUE(wait, current);
  266. /* Prevent setting state FL_SYNCING for chip in suspended state. */
  267. if (FL_SYNCING == mode && FL_READY != chip->oldstate)
  268. goto sleep;
  269. switch (chip->state) {
  270. case FL_READY:
  271. case FL_JEDEC_QUERY:
  272. return 0;
  273. case FL_ERASING:
  274. if (!lpddr->qinfo->SuspEraseSupp ||
  275. !(mode == FL_READY || mode == FL_POINT))
  276. goto sleep;
  277. map_write(map, CMD(LPDDR_SUSPEND),
  278. map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
  279. chip->oldstate = FL_ERASING;
  280. chip->state = FL_ERASE_SUSPENDING;
  281. ret = wait_for_ready(map, chip, 0);
  282. if (ret) {
  283. /* Oops. something got wrong. */
  284. /* Resume and pretend we weren't here. */
  285. put_chip(map, chip);
  286. printk(KERN_ERR "%s: suspend operation failed."
  287. "State may be wrong \n", map->name);
  288. return -EIO;
  289. }
  290. chip->erase_suspended = 1;
  291. chip->state = FL_READY;
  292. return 0;
  293. /* Erase suspend */
  294. case FL_POINT:
  295. /* Only if there's no operation suspended... */
  296. if (mode == FL_READY && chip->oldstate == FL_READY)
  297. return 0;
  298. default:
  299. sleep:
  300. set_current_state(TASK_UNINTERRUPTIBLE);
  301. add_wait_queue(&chip->wq, &wait);
  302. mutex_unlock(&chip->mutex);
  303. schedule();
  304. remove_wait_queue(&chip->wq, &wait);
  305. mutex_lock(&chip->mutex);
  306. return -EAGAIN;
  307. }
  308. }
  309. static void put_chip(struct map_info *map, struct flchip *chip)
  310. {
  311. if (chip->priv) {
  312. struct flchip_shared *shared = chip->priv;
  313. mutex_lock(&shared->lock);
  314. if (shared->writing == chip && chip->oldstate == FL_READY) {
  315. /* We own the ability to write, but we're done */
  316. shared->writing = shared->erasing;
  317. if (shared->writing && shared->writing != chip) {
  318. /* give back the ownership */
  319. struct flchip *loaner = shared->writing;
  320. mutex_lock(&loaner->mutex);
  321. mutex_unlock(&shared->lock);
  322. mutex_unlock(&chip->mutex);
  323. put_chip(map, loaner);
  324. mutex_lock(&chip->mutex);
  325. mutex_unlock(&loaner->mutex);
  326. wake_up(&chip->wq);
  327. return;
  328. }
  329. shared->erasing = NULL;
  330. shared->writing = NULL;
  331. } else if (shared->erasing == chip && shared->writing != chip) {
  332. /*
  333. * We own the ability to erase without the ability
  334. * to write, which means the erase was suspended
  335. * and some other partition is currently writing.
  336. * Don't let the switch below mess things up since
  337. * we don't have ownership to resume anything.
  338. */
  339. mutex_unlock(&shared->lock);
  340. wake_up(&chip->wq);
  341. return;
  342. }
  343. mutex_unlock(&shared->lock);
  344. }
  345. switch (chip->oldstate) {
  346. case FL_ERASING:
  347. map_write(map, CMD(LPDDR_RESUME),
  348. map->pfow_base + PFOW_COMMAND_CODE);
  349. map_write(map, CMD(LPDDR_START_EXECUTION),
  350. map->pfow_base + PFOW_COMMAND_EXECUTE);
  351. chip->oldstate = FL_READY;
  352. chip->state = FL_ERASING;
  353. break;
  354. case FL_READY:
  355. break;
  356. default:
  357. printk(KERN_ERR "%s: put_chip() called with oldstate %d!\n",
  358. map->name, chip->oldstate);
  359. }
  360. wake_up(&chip->wq);
  361. }
  362. int do_write_buffer(struct map_info *map, struct flchip *chip,
  363. unsigned long adr, const struct kvec **pvec,
  364. unsigned long *pvec_seek, int len)
  365. {
  366. struct lpddr_private *lpddr = map->fldrv_priv;
  367. map_word datum;
  368. int ret, wbufsize, word_gap, words;
  369. const struct kvec *vec;
  370. unsigned long vec_seek;
  371. unsigned long prog_buf_ofs;
  372. wbufsize = 1 << lpddr->qinfo->BufSizeShift;
  373. mutex_lock(&chip->mutex);
  374. ret = get_chip(map, chip, FL_WRITING);
  375. if (ret) {
  376. mutex_unlock(&chip->mutex);
  377. return ret;
  378. }
  379. /* Figure out the number of words to write */
  380. word_gap = (-adr & (map_bankwidth(map)-1));
  381. words = (len - word_gap + map_bankwidth(map) - 1) / map_bankwidth(map);
  382. if (!word_gap) {
  383. words--;
  384. } else {
  385. word_gap = map_bankwidth(map) - word_gap;
  386. adr -= word_gap;
  387. datum = map_word_ff(map);
  388. }
  389. /* Write data */
  390. /* Get the program buffer offset from PFOW register data first*/
  391. prog_buf_ofs = map->pfow_base + CMDVAL(map_read(map,
  392. map->pfow_base + PFOW_PROGRAM_BUFFER_OFFSET));
  393. vec = *pvec;
  394. vec_seek = *pvec_seek;
  395. do {
  396. int n = map_bankwidth(map) - word_gap;
  397. if (n > vec->iov_len - vec_seek)
  398. n = vec->iov_len - vec_seek;
  399. if (n > len)
  400. n = len;
  401. if (!word_gap && (len < map_bankwidth(map)))
  402. datum = map_word_ff(map);
  403. datum = map_word_load_partial(map, datum,
  404. vec->iov_base + vec_seek, word_gap, n);
  405. len -= n;
  406. word_gap += n;
  407. if (!len || word_gap == map_bankwidth(map)) {
  408. map_write(map, datum, prog_buf_ofs);
  409. prog_buf_ofs += map_bankwidth(map);
  410. word_gap = 0;
  411. }
  412. vec_seek += n;
  413. if (vec_seek == vec->iov_len) {
  414. vec++;
  415. vec_seek = 0;
  416. }
  417. } while (len);
  418. *pvec = vec;
  419. *pvec_seek = vec_seek;
  420. /* GO GO GO */
  421. send_pfow_command(map, LPDDR_BUFF_PROGRAM, adr, wbufsize, NULL);
  422. chip->state = FL_WRITING;
  423. ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->ProgBufferTime));
  424. if (ret) {
  425. printk(KERN_WARNING"%s Buffer program error: %d at %lx; \n",
  426. map->name, ret, adr);
  427. goto out;
  428. }
  429. out: put_chip(map, chip);
  430. mutex_unlock(&chip->mutex);
  431. return ret;
  432. }
  433. int do_erase_oneblock(struct mtd_info *mtd, loff_t adr)
  434. {
  435. struct map_info *map = mtd->priv;
  436. struct lpddr_private *lpddr = map->fldrv_priv;
  437. int chipnum = adr >> lpddr->chipshift;
  438. struct flchip *chip = &lpddr->chips[chipnum];
  439. int ret;
  440. mutex_lock(&chip->mutex);
  441. ret = get_chip(map, chip, FL_ERASING);
  442. if (ret) {
  443. mutex_unlock(&chip->mutex);
  444. return ret;
  445. }
  446. send_pfow_command(map, LPDDR_BLOCK_ERASE, adr, 0, NULL);
  447. chip->state = FL_ERASING;
  448. ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->BlockEraseTime)*1000);
  449. if (ret) {
  450. printk(KERN_WARNING"%s Erase block error %d at : %llx\n",
  451. map->name, ret, adr);
  452. goto out;
  453. }
  454. out: put_chip(map, chip);
  455. mutex_unlock(&chip->mutex);
  456. return ret;
  457. }
  458. static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
  459. size_t *retlen, u_char *buf)
  460. {
  461. struct map_info *map = mtd->priv;
  462. struct lpddr_private *lpddr = map->fldrv_priv;
  463. int chipnum = adr >> lpddr->chipshift;
  464. struct flchip *chip = &lpddr->chips[chipnum];
  465. int ret = 0;
  466. mutex_lock(&chip->mutex);
  467. ret = get_chip(map, chip, FL_READY);
  468. if (ret) {
  469. mutex_unlock(&chip->mutex);
  470. return ret;
  471. }
  472. map_copy_from(map, buf, adr, len);
  473. *retlen = len;
  474. put_chip(map, chip);
  475. mutex_unlock(&chip->mutex);
  476. return ret;
  477. }
  478. static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
  479. size_t *retlen, void **mtdbuf, resource_size_t *phys)
  480. {
  481. struct map_info *map = mtd->priv;
  482. struct lpddr_private *lpddr = map->fldrv_priv;
  483. int chipnum = adr >> lpddr->chipshift;
  484. unsigned long ofs, last_end = 0;
  485. struct flchip *chip = &lpddr->chips[chipnum];
  486. int ret = 0;
  487. if (!map->virt)
  488. return -EINVAL;
  489. /* ofs: offset within the first chip that the first read should start */
  490. ofs = adr - (chipnum << lpddr->chipshift);
  491. *mtdbuf = (void *)map->virt + chip->start + ofs;
  492. while (len) {
  493. unsigned long thislen;
  494. if (chipnum >= lpddr->numchips)
  495. break;
  496. /* We cannot point across chips that are virtually disjoint */
  497. if (!last_end)
  498. last_end = chip->start;
  499. else if (chip->start != last_end)
  500. break;
  501. if ((len + ofs - 1) >> lpddr->chipshift)
  502. thislen = (1<<lpddr->chipshift) - ofs;
  503. else
  504. thislen = len;
  505. /* get the chip */
  506. mutex_lock(&chip->mutex);
  507. ret = get_chip(map, chip, FL_POINT);
  508. mutex_unlock(&chip->mutex);
  509. if (ret)
  510. break;
  511. chip->state = FL_POINT;
  512. chip->ref_point_counter++;
  513. *retlen += thislen;
  514. len -= thislen;
  515. ofs = 0;
  516. last_end += 1 << lpddr->chipshift;
  517. chipnum++;
  518. chip = &lpddr->chips[chipnum];
  519. }
  520. return 0;
  521. }
  522. static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
  523. {
  524. struct map_info *map = mtd->priv;
  525. struct lpddr_private *lpddr = map->fldrv_priv;
  526. int chipnum = adr >> lpddr->chipshift, err = 0;
  527. unsigned long ofs;
  528. /* ofs: offset within the first chip that the first read should start */
  529. ofs = adr - (chipnum << lpddr->chipshift);
  530. while (len) {
  531. unsigned long thislen;
  532. struct flchip *chip;
  533. chip = &lpddr->chips[chipnum];
  534. if (chipnum >= lpddr->numchips)
  535. break;
  536. if ((len + ofs - 1) >> lpddr->chipshift)
  537. thislen = (1<<lpddr->chipshift) - ofs;
  538. else
  539. thislen = len;
  540. mutex_lock(&chip->mutex);
  541. if (chip->state == FL_POINT) {
  542. chip->ref_point_counter--;
  543. if (chip->ref_point_counter == 0)
  544. chip->state = FL_READY;
  545. } else {
  546. printk(KERN_WARNING "%s: Warning: unpoint called on non"
  547. "pointed region\n", map->name);
  548. err = -EINVAL;
  549. }
  550. put_chip(map, chip);
  551. mutex_unlock(&chip->mutex);
  552. len -= thislen;
  553. ofs = 0;
  554. chipnum++;
  555. }
  556. return err;
  557. }
  558. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  559. size_t *retlen, const u_char *buf)
  560. {
  561. struct kvec vec;
  562. vec.iov_base = (void *) buf;
  563. vec.iov_len = len;
  564. return lpddr_writev(mtd, &vec, 1, to, retlen);
  565. }
  566. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  567. unsigned long count, loff_t to, size_t *retlen)
  568. {
  569. struct map_info *map = mtd->priv;
  570. struct lpddr_private *lpddr = map->fldrv_priv;
  571. int ret = 0;
  572. int chipnum;
  573. unsigned long ofs, vec_seek, i;
  574. int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
  575. size_t len = 0;
  576. for (i = 0; i < count; i++)
  577. len += vecs[i].iov_len;
  578. if (!len)
  579. return 0;
  580. chipnum = to >> lpddr->chipshift;
  581. ofs = to;
  582. vec_seek = 0;
  583. do {
  584. /* We must not cross write block boundaries */
  585. int size = wbufsize - (ofs & (wbufsize-1));
  586. if (size > len)
  587. size = len;
  588. ret = do_write_buffer(map, &lpddr->chips[chipnum],
  589. ofs, &vecs, &vec_seek, size);
  590. if (ret)
  591. return ret;
  592. ofs += size;
  593. (*retlen) += size;
  594. len -= size;
  595. /* Be nice and reschedule with the chip in a usable
  596. * state for other processes */
  597. cond_resched();
  598. } while (len);
  599. return 0;
  600. }
  601. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
  602. {
  603. unsigned long ofs, len;
  604. int ret;
  605. struct map_info *map = mtd->priv;
  606. struct lpddr_private *lpddr = map->fldrv_priv;
  607. int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
  608. ofs = instr->addr;
  609. len = instr->len;
  610. while (len > 0) {
  611. ret = do_erase_oneblock(mtd, ofs);
  612. if (ret)
  613. return ret;
  614. ofs += size;
  615. len -= size;
  616. }
  617. instr->state = MTD_ERASE_DONE;
  618. mtd_erase_callback(instr);
  619. return 0;
  620. }
  621. #define DO_XXLOCK_LOCK 1
  622. #define DO_XXLOCK_UNLOCK 2
  623. int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
  624. {
  625. int ret = 0;
  626. struct map_info *map = mtd->priv;
  627. struct lpddr_private *lpddr = map->fldrv_priv;
  628. int chipnum = adr >> lpddr->chipshift;
  629. struct flchip *chip = &lpddr->chips[chipnum];
  630. mutex_lock(&chip->mutex);
  631. ret = get_chip(map, chip, FL_LOCKING);
  632. if (ret) {
  633. mutex_unlock(&chip->mutex);
  634. return ret;
  635. }
  636. if (thunk == DO_XXLOCK_LOCK) {
  637. send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
  638. chip->state = FL_LOCKING;
  639. } else if (thunk == DO_XXLOCK_UNLOCK) {
  640. send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
  641. chip->state = FL_UNLOCKING;
  642. } else
  643. BUG();
  644. ret = wait_for_ready(map, chip, 1);
  645. if (ret) {
  646. printk(KERN_ERR "%s: block unlock error status %d \n",
  647. map->name, ret);
  648. goto out;
  649. }
  650. out: put_chip(map, chip);
  651. mutex_unlock(&chip->mutex);
  652. return ret;
  653. }
  654. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  655. {
  656. return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
  657. }
  658. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  659. {
  660. return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
  661. }
  662. int word_program(struct map_info *map, loff_t adr, uint32_t curval)
  663. {
  664. int ret;
  665. struct lpddr_private *lpddr = map->fldrv_priv;
  666. int chipnum = adr >> lpddr->chipshift;
  667. struct flchip *chip = &lpddr->chips[chipnum];
  668. mutex_lock(&chip->mutex);
  669. ret = get_chip(map, chip, FL_WRITING);
  670. if (ret) {
  671. mutex_unlock(&chip->mutex);
  672. return ret;
  673. }
  674. send_pfow_command(map, LPDDR_WORD_PROGRAM, adr, 0x00, (map_word *)&curval);
  675. ret = wait_for_ready(map, chip, (1<<lpddr->qinfo->SingleWordProgTime));
  676. if (ret) {
  677. printk(KERN_WARNING"%s word_program error at: %llx; val: %x\n",
  678. map->name, adr, curval);
  679. goto out;
  680. }
  681. out: put_chip(map, chip);
  682. mutex_unlock(&chip->mutex);
  683. return ret;
  684. }
  685. MODULE_LICENSE("GPL");
  686. MODULE_AUTHOR("Alexey Korolev <akorolev@infradead.org>");
  687. MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");