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