lpddr_cmds.c 21 KB

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