lpddr_cmds.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LPDDR flash memory device operations. This module provides read, write,
  4. * erase, lock/unlock support for LPDDR flash memories
  5. * (C) 2008 Korolev Alexey <[email protected]>
  6. * (C) 2008 Vasiliy Leonenko <[email protected]>
  7. * Many thanks to Roman Borisov for initial enabling
  8. *
  9. * TODO:
  10. * Implement VPP management
  11. * Implement XIP support
  12. * Implement OTP support
  13. */
  14. #include <linux/mtd/pfow.h>
  15. #include <linux/mtd/qinfo.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. static int lpddr_read(struct mtd_info *mtd, loff_t adr, size_t len,
  19. size_t *retlen, u_char *buf);
  20. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to,
  21. size_t len, size_t *retlen, const u_char *buf);
  22. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  23. unsigned long count, loff_t to, size_t *retlen);
  24. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr);
  25. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  26. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
  27. static int lpddr_point(struct mtd_info *mtd, loff_t adr, size_t len,
  28. size_t *retlen, void **mtdbuf, resource_size_t *phys);
  29. static int lpddr_unpoint(struct mtd_info *mtd, loff_t adr, size_t len);
  30. static int get_chip(struct map_info *map, struct flchip *chip, int mode);
  31. static int chip_ready(struct map_info *map, struct flchip *chip, int mode);
  32. static void put_chip(struct map_info *map, struct flchip *chip);
  33. struct mtd_info *lpddr_cmdset(struct map_info *map)
  34. {
  35. struct lpddr_private *lpddr = map->fldrv_priv;
  36. struct flchip_shared *shared;
  37. struct flchip *chip;
  38. struct mtd_info *mtd;
  39. int numchips;
  40. int i, j;
  41. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  42. if (!mtd)
  43. return NULL;
  44. mtd->priv = map;
  45. mtd->type = MTD_NORFLASH;
  46. /* Fill in the default mtd operations */
  47. mtd->_read = lpddr_read;
  48. mtd->type = MTD_NORFLASH;
  49. mtd->flags = MTD_CAP_NORFLASH;
  50. mtd->flags &= ~MTD_BIT_WRITEABLE;
  51. mtd->_erase = lpddr_erase;
  52. mtd->_write = lpddr_write_buffers;
  53. mtd->_writev = lpddr_writev;
  54. mtd->_lock = lpddr_lock;
  55. mtd->_unlock = lpddr_unlock;
  56. if (map_is_linear(map)) {
  57. mtd->_point = lpddr_point;
  58. mtd->_unpoint = lpddr_unpoint;
  59. }
  60. mtd->size = 1 << lpddr->qinfo->DevSizeShift;
  61. mtd->erasesize = 1 << lpddr->qinfo->UniformBlockSizeShift;
  62. mtd->writesize = 1 << lpddr->qinfo->BufSizeShift;
  63. shared = kmalloc_array(lpddr->numchips, sizeof(struct flchip_shared),
  64. GFP_KERNEL);
  65. if (!shared) {
  66. kfree(mtd);
  67. return NULL;
  68. }
  69. chip = &lpddr->chips[0];
  70. numchips = lpddr->numchips / lpddr->qinfo->HWPartsNum;
  71. for (i = 0; i < numchips; i++) {
  72. shared[i].writing = shared[i].erasing = NULL;
  73. mutex_init(&shared[i].lock);
  74. for (j = 0; j < lpddr->qinfo->HWPartsNum; j++) {
  75. *chip = lpddr->chips[i];
  76. chip->start += j << lpddr->chipshift;
  77. chip->oldstate = chip->state = FL_READY;
  78. chip->priv = &shared[i];
  79. /* those should be reset too since
  80. they create memory references. */
  81. init_waitqueue_head(&chip->wq);
  82. mutex_init(&chip->mutex);
  83. chip++;
  84. }
  85. }
  86. return mtd;
  87. }
  88. EXPORT_SYMBOL(lpddr_cmdset);
  89. static void print_drs_error(unsigned int dsr)
  90. {
  91. int prog_status = (dsr & DSR_RPS) >> 8;
  92. if (!(dsr & DSR_AVAILABLE))
  93. pr_notice("DSR.15: (0) Device not Available\n");
  94. if ((prog_status & 0x03) == 0x03)
  95. pr_notice("DSR.9,8: (11) Attempt to program invalid half with 41h command\n");
  96. else if (prog_status & 0x02)
  97. pr_notice("DSR.9,8: (10) Object Mode Program attempt in region with Control Mode data\n");
  98. else if (prog_status & 0x01)
  99. pr_notice("DSR.9,8: (01) Program attempt in region with Object Mode data\n");
  100. if (!(dsr & DSR_READY_STATUS))
  101. pr_notice("DSR.7: (0) Device is Busy\n");
  102. if (dsr & DSR_ESS)
  103. pr_notice("DSR.6: (1) Erase Suspended\n");
  104. if (dsr & DSR_ERASE_STATUS)
  105. pr_notice("DSR.5: (1) Erase/Blank check error\n");
  106. if (dsr & DSR_PROGRAM_STATUS)
  107. pr_notice("DSR.4: (1) Program Error\n");
  108. if (dsr & DSR_VPPS)
  109. pr_notice("DSR.3: (1) Vpp low detect, operation aborted\n");
  110. if (dsr & DSR_PSS)
  111. pr_notice("DSR.2: (1) Program suspended\n");
  112. if (dsr & DSR_DPS)
  113. pr_notice("DSR.1: (1) Aborted Erase/Program attempt on locked block\n");
  114. }
  115. static int wait_for_ready(struct map_info *map, struct flchip *chip,
  116. unsigned int chip_op_time)
  117. {
  118. unsigned int timeo, reset_timeo, sleep_time;
  119. unsigned int dsr;
  120. flstate_t chip_state = chip->state;
  121. int ret = 0;
  122. /* set our timeout to 8 times the expected delay */
  123. timeo = chip_op_time * 8;
  124. if (!timeo)
  125. timeo = 500000;
  126. reset_timeo = timeo;
  127. sleep_time = chip_op_time / 2;
  128. for (;;) {
  129. dsr = CMDVAL(map_read(map, map->pfow_base + PFOW_DSR));
  130. if (dsr & DSR_READY_STATUS)
  131. break;
  132. if (!timeo) {
  133. printk(KERN_ERR "%s: Flash timeout error state %d \n",
  134. map->name, chip_state);
  135. ret = -ETIME;
  136. break;
  137. }
  138. /* OK Still waiting. Drop the lock, wait a while and retry. */
  139. mutex_unlock(&chip->mutex);
  140. if (sleep_time >= 1000000/HZ) {
  141. /*
  142. * Half of the normal delay still remaining
  143. * can be performed with a sleeping delay instead
  144. * of busy waiting.
  145. */
  146. msleep(sleep_time/1000);
  147. timeo -= sleep_time;
  148. sleep_time = 1000000/HZ;
  149. } else {
  150. udelay(1);
  151. cond_resched();
  152. timeo--;
  153. }
  154. mutex_lock(&chip->mutex);
  155. while (chip->state != chip_state) {
  156. /* Someone's suspended the operation: sleep */
  157. DECLARE_WAITQUEUE(wait, current);
  158. set_current_state(TASK_UNINTERRUPTIBLE);
  159. add_wait_queue(&chip->wq, &wait);
  160. mutex_unlock(&chip->mutex);
  161. schedule();
  162. remove_wait_queue(&chip->wq, &wait);
  163. mutex_lock(&chip->mutex);
  164. }
  165. if (chip->erase_suspended || chip->write_suspended) {
  166. /* Suspend has occurred while sleep: reset timeout */
  167. timeo = reset_timeo;
  168. chip->erase_suspended = chip->write_suspended = 0;
  169. }
  170. }
  171. /* check status for errors */
  172. if (dsr & DSR_ERR) {
  173. /* Clear DSR*/
  174. map_write(map, CMD(~(DSR_ERR)), map->pfow_base + PFOW_DSR);
  175. printk(KERN_WARNING"%s: Bad status on wait: 0x%x \n",
  176. map->name, dsr);
  177. print_drs_error(dsr);
  178. ret = -EIO;
  179. }
  180. chip->state = FL_READY;
  181. return ret;
  182. }
  183. static int get_chip(struct map_info *map, struct flchip *chip, int mode)
  184. {
  185. int ret;
  186. DECLARE_WAITQUEUE(wait, current);
  187. retry:
  188. if (chip->priv && (mode == FL_WRITING || mode == FL_ERASING)
  189. && chip->state != FL_SYNCING) {
  190. /*
  191. * OK. We have possibility for contension on the write/erase
  192. * operations which are global to the real chip and not per
  193. * partition. So let's fight it over in the partition which
  194. * currently has authority on the operation.
  195. *
  196. * The rules are as follows:
  197. *
  198. * - any write operation must own shared->writing.
  199. *
  200. * - any erase operation must own _both_ shared->writing and
  201. * shared->erasing.
  202. *
  203. * - contension arbitration is handled in the owner's context.
  204. *
  205. * The 'shared' struct can be read and/or written only when
  206. * its lock is taken.
  207. */
  208. struct flchip_shared *shared = chip->priv;
  209. struct flchip *contender;
  210. mutex_lock(&shared->lock);
  211. contender = shared->writing;
  212. if (contender && contender != chip) {
  213. /*
  214. * The engine to perform desired operation on this
  215. * partition is already in use by someone else.
  216. * Let's fight over it in the context of the chip
  217. * currently using it. If it is possible to suspend,
  218. * that other partition will do just that, otherwise
  219. * it'll happily send us to sleep. In any case, when
  220. * get_chip returns success we're clear to go ahead.
  221. */
  222. ret = mutex_trylock(&contender->mutex);
  223. mutex_unlock(&shared->lock);
  224. if (!ret)
  225. goto retry;
  226. mutex_unlock(&chip->mutex);
  227. ret = chip_ready(map, contender, mode);
  228. mutex_lock(&chip->mutex);
  229. if (ret == -EAGAIN) {
  230. mutex_unlock(&contender->mutex);
  231. goto retry;
  232. }
  233. if (ret) {
  234. mutex_unlock(&contender->mutex);
  235. return ret;
  236. }
  237. mutex_lock(&shared->lock);
  238. /* We should not own chip if it is already in FL_SYNCING
  239. * state. Put contender and retry. */
  240. if (chip->state == FL_SYNCING) {
  241. put_chip(map, contender);
  242. mutex_unlock(&contender->mutex);
  243. goto retry;
  244. }
  245. mutex_unlock(&contender->mutex);
  246. }
  247. /* Check if we have suspended erase on this chip.
  248. Must sleep in such a case. */
  249. if (mode == FL_ERASING && shared->erasing
  250. && shared->erasing->oldstate == FL_ERASING) {
  251. mutex_unlock(&shared->lock);
  252. set_current_state(TASK_UNINTERRUPTIBLE);
  253. add_wait_queue(&chip->wq, &wait);
  254. mutex_unlock(&chip->mutex);
  255. schedule();
  256. remove_wait_queue(&chip->wq, &wait);
  257. mutex_lock(&chip->mutex);
  258. goto retry;
  259. }
  260. /* We now own it */
  261. shared->writing = chip;
  262. if (mode == FL_ERASING)
  263. shared->erasing = chip;
  264. mutex_unlock(&shared->lock);
  265. }
  266. ret = chip_ready(map, chip, mode);
  267. if (ret == -EAGAIN)
  268. goto retry;
  269. return ret;
  270. }
  271. static int chip_ready(struct map_info *map, struct flchip *chip, int mode)
  272. {
  273. struct lpddr_private *lpddr = map->fldrv_priv;
  274. int ret = 0;
  275. DECLARE_WAITQUEUE(wait, current);
  276. /* Prevent setting state FL_SYNCING for chip in suspended state. */
  277. if (FL_SYNCING == mode && FL_READY != chip->oldstate)
  278. goto sleep;
  279. switch (chip->state) {
  280. case FL_READY:
  281. case FL_JEDEC_QUERY:
  282. return 0;
  283. case FL_ERASING:
  284. if (!lpddr->qinfo->SuspEraseSupp ||
  285. !(mode == FL_READY || mode == FL_POINT))
  286. goto sleep;
  287. map_write(map, CMD(LPDDR_SUSPEND),
  288. map->pfow_base + PFOW_PROGRAM_ERASE_SUSPEND);
  289. chip->oldstate = FL_ERASING;
  290. chip->state = FL_ERASE_SUSPENDING;
  291. ret = wait_for_ready(map, chip, 0);
  292. if (ret) {
  293. /* Oops. something got wrong. */
  294. /* Resume and pretend we weren't here. */
  295. put_chip(map, chip);
  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. fallthrough;
  309. default:
  310. sleep:
  311. set_current_state(TASK_UNINTERRUPTIBLE);
  312. add_wait_queue(&chip->wq, &wait);
  313. mutex_unlock(&chip->mutex);
  314. schedule();
  315. remove_wait_queue(&chip->wq, &wait);
  316. mutex_lock(&chip->mutex);
  317. return -EAGAIN;
  318. }
  319. }
  320. static void put_chip(struct map_info *map, struct flchip *chip)
  321. {
  322. if (chip->priv) {
  323. struct flchip_shared *shared = chip->priv;
  324. mutex_lock(&shared->lock);
  325. if (shared->writing == chip && chip->oldstate == FL_READY) {
  326. /* We own the ability to write, but we're done */
  327. shared->writing = shared->erasing;
  328. if (shared->writing && shared->writing != chip) {
  329. /* give back the ownership */
  330. struct flchip *loaner = shared->writing;
  331. mutex_lock(&loaner->mutex);
  332. mutex_unlock(&shared->lock);
  333. mutex_unlock(&chip->mutex);
  334. put_chip(map, loaner);
  335. mutex_lock(&chip->mutex);
  336. mutex_unlock(&loaner->mutex);
  337. wake_up(&chip->wq);
  338. return;
  339. }
  340. shared->erasing = NULL;
  341. shared->writing = NULL;
  342. } else if (shared->erasing == chip && shared->writing != chip) {
  343. /*
  344. * We own the ability to erase without the ability
  345. * to write, which means the erase was suspended
  346. * and some other partition is currently writing.
  347. * Don't let the switch below mess things up since
  348. * we don't have ownership to resume anything.
  349. */
  350. mutex_unlock(&shared->lock);
  351. wake_up(&chip->wq);
  352. return;
  353. }
  354. mutex_unlock(&shared->lock);
  355. }
  356. switch (chip->oldstate) {
  357. case FL_ERASING:
  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. static 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. static 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)
  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. while (len) {
  504. unsigned long thislen;
  505. if (chipnum >= lpddr->numchips)
  506. break;
  507. /* We cannot point across chips that are virtually disjoint */
  508. if (!last_end)
  509. last_end = chip->start;
  510. else if (chip->start != last_end)
  511. break;
  512. if ((len + ofs - 1) >> lpddr->chipshift)
  513. thislen = (1<<lpddr->chipshift) - ofs;
  514. else
  515. thislen = len;
  516. /* get the chip */
  517. mutex_lock(&chip->mutex);
  518. ret = get_chip(map, chip, FL_POINT);
  519. mutex_unlock(&chip->mutex);
  520. if (ret)
  521. break;
  522. chip->state = FL_POINT;
  523. chip->ref_point_counter++;
  524. *retlen += thislen;
  525. len -= thislen;
  526. ofs = 0;
  527. last_end += 1 << lpddr->chipshift;
  528. chipnum++;
  529. chip = &lpddr->chips[chipnum];
  530. }
  531. return 0;
  532. }
  533. static int lpddr_unpoint (struct mtd_info *mtd, loff_t adr, size_t len)
  534. {
  535. struct map_info *map = mtd->priv;
  536. struct lpddr_private *lpddr = map->fldrv_priv;
  537. int chipnum = adr >> lpddr->chipshift, err = 0;
  538. unsigned long ofs;
  539. /* ofs: offset within the first chip that the first read should start */
  540. ofs = adr - (chipnum << lpddr->chipshift);
  541. while (len) {
  542. unsigned long thislen;
  543. struct flchip *chip;
  544. chip = &lpddr->chips[chipnum];
  545. if (chipnum >= lpddr->numchips)
  546. break;
  547. if ((len + ofs - 1) >> lpddr->chipshift)
  548. thislen = (1<<lpddr->chipshift) - ofs;
  549. else
  550. thislen = len;
  551. mutex_lock(&chip->mutex);
  552. if (chip->state == FL_POINT) {
  553. chip->ref_point_counter--;
  554. if (chip->ref_point_counter == 0)
  555. chip->state = FL_READY;
  556. } else {
  557. printk(KERN_WARNING "%s: Warning: unpoint called on non"
  558. "pointed region\n", map->name);
  559. err = -EINVAL;
  560. }
  561. put_chip(map, chip);
  562. mutex_unlock(&chip->mutex);
  563. len -= thislen;
  564. ofs = 0;
  565. chipnum++;
  566. }
  567. return err;
  568. }
  569. static int lpddr_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
  570. size_t *retlen, const u_char *buf)
  571. {
  572. struct kvec vec;
  573. vec.iov_base = (void *) buf;
  574. vec.iov_len = len;
  575. return lpddr_writev(mtd, &vec, 1, to, retlen);
  576. }
  577. static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
  578. unsigned long count, loff_t to, size_t *retlen)
  579. {
  580. struct map_info *map = mtd->priv;
  581. struct lpddr_private *lpddr = map->fldrv_priv;
  582. int ret = 0;
  583. int chipnum;
  584. unsigned long ofs, vec_seek, i;
  585. int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
  586. size_t len = 0;
  587. for (i = 0; i < count; i++)
  588. len += vecs[i].iov_len;
  589. if (!len)
  590. return 0;
  591. chipnum = to >> lpddr->chipshift;
  592. ofs = to;
  593. vec_seek = 0;
  594. do {
  595. /* We must not cross write block boundaries */
  596. int size = wbufsize - (ofs & (wbufsize-1));
  597. if (size > len)
  598. size = len;
  599. ret = do_write_buffer(map, &lpddr->chips[chipnum],
  600. ofs, &vecs, &vec_seek, size);
  601. if (ret)
  602. return ret;
  603. ofs += size;
  604. (*retlen) += size;
  605. len -= size;
  606. /* Be nice and reschedule with the chip in a usable
  607. * state for other processes */
  608. cond_resched();
  609. } while (len);
  610. return 0;
  611. }
  612. static int lpddr_erase(struct mtd_info *mtd, struct erase_info *instr)
  613. {
  614. unsigned long ofs, len;
  615. int ret;
  616. struct map_info *map = mtd->priv;
  617. struct lpddr_private *lpddr = map->fldrv_priv;
  618. int size = 1 << lpddr->qinfo->UniformBlockSizeShift;
  619. ofs = instr->addr;
  620. len = instr->len;
  621. while (len > 0) {
  622. ret = do_erase_oneblock(mtd, ofs);
  623. if (ret)
  624. return ret;
  625. ofs += size;
  626. len -= size;
  627. }
  628. return 0;
  629. }
  630. #define DO_XXLOCK_LOCK 1
  631. #define DO_XXLOCK_UNLOCK 2
  632. static int do_xxlock(struct mtd_info *mtd, loff_t adr, uint32_t len, int thunk)
  633. {
  634. int ret = 0;
  635. struct map_info *map = mtd->priv;
  636. struct lpddr_private *lpddr = map->fldrv_priv;
  637. int chipnum = adr >> lpddr->chipshift;
  638. struct flchip *chip = &lpddr->chips[chipnum];
  639. mutex_lock(&chip->mutex);
  640. ret = get_chip(map, chip, FL_LOCKING);
  641. if (ret) {
  642. mutex_unlock(&chip->mutex);
  643. return ret;
  644. }
  645. if (thunk == DO_XXLOCK_LOCK) {
  646. send_pfow_command(map, LPDDR_LOCK_BLOCK, adr, adr + len, NULL);
  647. chip->state = FL_LOCKING;
  648. } else if (thunk == DO_XXLOCK_UNLOCK) {
  649. send_pfow_command(map, LPDDR_UNLOCK_BLOCK, adr, adr + len, NULL);
  650. chip->state = FL_UNLOCKING;
  651. } else
  652. BUG();
  653. ret = wait_for_ready(map, chip, 1);
  654. if (ret) {
  655. printk(KERN_ERR "%s: block unlock error status %d \n",
  656. map->name, ret);
  657. goto out;
  658. }
  659. out: put_chip(map, chip);
  660. mutex_unlock(&chip->mutex);
  661. return ret;
  662. }
  663. static int lpddr_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  664. {
  665. return do_xxlock(mtd, ofs, len, DO_XXLOCK_LOCK);
  666. }
  667. static int lpddr_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  668. {
  669. return do_xxlock(mtd, ofs, len, DO_XXLOCK_UNLOCK);
  670. }
  671. MODULE_LICENSE("GPL");
  672. MODULE_AUTHOR("Alexey Korolev <[email protected]>");
  673. MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");