lpddr_cmds.c 19 KB

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