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