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
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.
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.
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
23 * Implement VPP management
24 * Implement XIP support
25 * Implement OTP support
27 #include <linux/mtd/pfow.h>
28 #include <linux/mtd/qinfo.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
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
);
48 struct mtd_info
*lpddr_cmdset(struct map_info
*map
)
50 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
51 struct flchip_shared
*shared
;
57 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
59 printk(KERN_ERR
"Failed to allocate memory for MTD device\n");
63 mtd
->type
= MTD_NORFLASH
;
65 /* Fill in the default mtd operations */
66 mtd
->_read
= lpddr_read
;
67 mtd
->type
= MTD_NORFLASH
;
68 mtd
->flags
= MTD_CAP_NORFLASH
;
69 mtd
->flags
&= ~MTD_BIT_WRITEABLE
;
70 mtd
->_erase
= lpddr_erase
;
71 mtd
->_write
= lpddr_write_buffers
;
72 mtd
->_writev
= lpddr_writev
;
73 mtd
->_lock
= lpddr_lock
;
74 mtd
->_unlock
= lpddr_unlock
;
75 if (map_is_linear(map
)) {
76 mtd
->_point
= lpddr_point
;
77 mtd
->_unpoint
= lpddr_unpoint
;
79 mtd
->size
= 1 << lpddr
->qinfo
->DevSizeShift
;
80 mtd
->erasesize
= 1 << lpddr
->qinfo
->UniformBlockSizeShift
;
81 mtd
->writesize
= 1 << lpddr
->qinfo
->BufSizeShift
;
83 shared
= kmalloc(sizeof(struct flchip_shared
) * lpddr
->numchips
,
91 chip
= &lpddr
->chips
[0];
92 numchips
= lpddr
->numchips
/ lpddr
->qinfo
->HWPartsNum
;
93 for (i
= 0; i
< numchips
; i
++) {
94 shared
[i
].writing
= shared
[i
].erasing
= NULL
;
95 mutex_init(&shared
[i
].lock
);
96 for (j
= 0; j
< lpddr
->qinfo
->HWPartsNum
; j
++) {
97 *chip
= lpddr
->chips
[i
];
98 chip
->start
+= j
<< lpddr
->chipshift
;
99 chip
->oldstate
= chip
->state
= FL_READY
;
100 chip
->priv
= &shared
[i
];
101 /* those should be reset too since
102 they create memory references. */
103 init_waitqueue_head(&chip
->wq
);
104 mutex_init(&chip
->mutex
);
111 EXPORT_SYMBOL(lpddr_cmdset
);
113 static int wait_for_ready(struct map_info
*map
, struct flchip
*chip
,
114 unsigned int chip_op_time
)
116 unsigned int timeo
, reset_timeo
, sleep_time
;
118 flstate_t chip_state
= chip
->state
;
121 /* set our timeout to 8 times the expected delay */
122 timeo
= chip_op_time
* 8;
126 sleep_time
= chip_op_time
/ 2;
129 dsr
= CMDVAL(map_read(map
, map
->pfow_base
+ PFOW_DSR
));
130 if (dsr
& DSR_READY_STATUS
)
133 printk(KERN_ERR
"%s: Flash timeout error state %d \n",
134 map
->name
, chip_state
);
139 /* OK Still waiting. Drop the lock, wait a while and retry. */
140 mutex_unlock(&chip
->mutex
);
141 if (sleep_time
>= 1000000/HZ
) {
143 * Half of the normal delay still remaining
144 * can be performed with a sleeping delay instead
147 msleep(sleep_time
/1000);
149 sleep_time
= 1000000/HZ
;
155 mutex_lock(&chip
->mutex
);
157 while (chip
->state
!= chip_state
) {
158 /* Someone's suspended the operation: sleep */
159 DECLARE_WAITQUEUE(wait
, current
);
160 set_current_state(TASK_UNINTERRUPTIBLE
);
161 add_wait_queue(&chip
->wq
, &wait
);
162 mutex_unlock(&chip
->mutex
);
164 remove_wait_queue(&chip
->wq
, &wait
);
165 mutex_lock(&chip
->mutex
);
167 if (chip
->erase_suspended
|| chip
->write_suspended
) {
168 /* Suspend has occurred while sleep: reset timeout */
170 chip
->erase_suspended
= chip
->write_suspended
= 0;
173 /* check status for errors */
176 map_write(map
, CMD(~(DSR_ERR
)), map
->pfow_base
+ PFOW_DSR
);
177 printk(KERN_WARNING
"%s: Bad status on wait: 0x%x \n",
179 print_drs_error(dsr
);
182 chip
->state
= FL_READY
;
186 static int get_chip(struct map_info
*map
, struct flchip
*chip
, int mode
)
189 DECLARE_WAITQUEUE(wait
, current
);
192 if (chip
->priv
&& (mode
== FL_WRITING
|| mode
== FL_ERASING
)
193 && chip
->state
!= FL_SYNCING
) {
195 * OK. We have possibility for contension on the write/erase
196 * operations which are global to the real chip and not per
197 * partition. So let's fight it over in the partition which
198 * currently has authority on the operation.
200 * The rules are as follows:
202 * - any write operation must own shared->writing.
204 * - any erase operation must own _both_ shared->writing and
207 * - contension arbitration is handled in the owner's context.
209 * The 'shared' struct can be read and/or written only when
212 struct flchip_shared
*shared
= chip
->priv
;
213 struct flchip
*contender
;
214 mutex_lock(&shared
->lock
);
215 contender
= shared
->writing
;
216 if (contender
&& contender
!= chip
) {
218 * The engine to perform desired operation on this
219 * partition is already in use by someone else.
220 * Let's fight over it in the context of the chip
221 * currently using it. If it is possible to suspend,
222 * that other partition will do just that, otherwise
223 * it'll happily send us to sleep. In any case, when
224 * get_chip returns success we're clear to go ahead.
226 ret
= mutex_trylock(&contender
->mutex
);
227 mutex_unlock(&shared
->lock
);
230 mutex_unlock(&chip
->mutex
);
231 ret
= chip_ready(map
, contender
, mode
);
232 mutex_lock(&chip
->mutex
);
234 if (ret
== -EAGAIN
) {
235 mutex_unlock(&contender
->mutex
);
239 mutex_unlock(&contender
->mutex
);
242 mutex_lock(&shared
->lock
);
244 /* We should not own chip if it is already in FL_SYNCING
245 * state. Put contender and retry. */
246 if (chip
->state
== FL_SYNCING
) {
247 put_chip(map
, contender
);
248 mutex_unlock(&contender
->mutex
);
251 mutex_unlock(&contender
->mutex
);
254 /* Check if we have suspended erase on this chip.
255 Must sleep in such a case. */
256 if (mode
== FL_ERASING
&& shared
->erasing
257 && shared
->erasing
->oldstate
== FL_ERASING
) {
258 mutex_unlock(&shared
->lock
);
259 set_current_state(TASK_UNINTERRUPTIBLE
);
260 add_wait_queue(&chip
->wq
, &wait
);
261 mutex_unlock(&chip
->mutex
);
263 remove_wait_queue(&chip
->wq
, &wait
);
264 mutex_lock(&chip
->mutex
);
269 shared
->writing
= chip
;
270 if (mode
== FL_ERASING
)
271 shared
->erasing
= chip
;
272 mutex_unlock(&shared
->lock
);
275 ret
= chip_ready(map
, chip
, mode
);
282 static int chip_ready(struct map_info
*map
, struct flchip
*chip
, int mode
)
284 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
286 DECLARE_WAITQUEUE(wait
, current
);
288 /* Prevent setting state FL_SYNCING for chip in suspended state. */
289 if (FL_SYNCING
== mode
&& FL_READY
!= chip
->oldstate
)
292 switch (chip
->state
) {
298 if (!lpddr
->qinfo
->SuspEraseSupp
||
299 !(mode
== FL_READY
|| mode
== FL_POINT
))
302 map_write(map
, CMD(LPDDR_SUSPEND
),
303 map
->pfow_base
+ PFOW_PROGRAM_ERASE_SUSPEND
);
304 chip
->oldstate
= FL_ERASING
;
305 chip
->state
= FL_ERASE_SUSPENDING
;
306 ret
= wait_for_ready(map
, chip
, 0);
308 /* Oops. something got wrong. */
309 /* Resume and pretend we weren't here. */
311 printk(KERN_ERR
"%s: suspend operation failed."
312 "State may be wrong \n", map
->name
);
315 chip
->erase_suspended
= 1;
316 chip
->state
= FL_READY
;
320 /* Only if there's no operation suspended... */
321 if (mode
== FL_READY
&& chip
->oldstate
== FL_READY
)
326 set_current_state(TASK_UNINTERRUPTIBLE
);
327 add_wait_queue(&chip
->wq
, &wait
);
328 mutex_unlock(&chip
->mutex
);
330 remove_wait_queue(&chip
->wq
, &wait
);
331 mutex_lock(&chip
->mutex
);
336 static void put_chip(struct map_info
*map
, struct flchip
*chip
)
339 struct flchip_shared
*shared
= chip
->priv
;
340 mutex_lock(&shared
->lock
);
341 if (shared
->writing
== chip
&& chip
->oldstate
== FL_READY
) {
342 /* We own the ability to write, but we're done */
343 shared
->writing
= shared
->erasing
;
344 if (shared
->writing
&& shared
->writing
!= chip
) {
345 /* give back the ownership */
346 struct flchip
*loaner
= shared
->writing
;
347 mutex_lock(&loaner
->mutex
);
348 mutex_unlock(&shared
->lock
);
349 mutex_unlock(&chip
->mutex
);
350 put_chip(map
, loaner
);
351 mutex_lock(&chip
->mutex
);
352 mutex_unlock(&loaner
->mutex
);
356 shared
->erasing
= NULL
;
357 shared
->writing
= NULL
;
358 } else if (shared
->erasing
== chip
&& shared
->writing
!= chip
) {
360 * We own the ability to erase without the ability
361 * to write, which means the erase was suspended
362 * and some other partition is currently writing.
363 * Don't let the switch below mess things up since
364 * we don't have ownership to resume anything.
366 mutex_unlock(&shared
->lock
);
370 mutex_unlock(&shared
->lock
);
373 switch (chip
->oldstate
) {
375 map_write(map
, CMD(LPDDR_RESUME
),
376 map
->pfow_base
+ PFOW_COMMAND_CODE
);
377 map_write(map
, CMD(LPDDR_START_EXECUTION
),
378 map
->pfow_base
+ PFOW_COMMAND_EXECUTE
);
379 chip
->oldstate
= FL_READY
;
380 chip
->state
= FL_ERASING
;
385 printk(KERN_ERR
"%s: put_chip() called with oldstate %d!\n",
386 map
->name
, chip
->oldstate
);
391 static int do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
392 unsigned long adr
, const struct kvec
**pvec
,
393 unsigned long *pvec_seek
, int len
)
395 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
397 int ret
, wbufsize
, word_gap
, words
;
398 const struct kvec
*vec
;
399 unsigned long vec_seek
;
400 unsigned long prog_buf_ofs
;
402 wbufsize
= 1 << lpddr
->qinfo
->BufSizeShift
;
404 mutex_lock(&chip
->mutex
);
405 ret
= get_chip(map
, chip
, FL_WRITING
);
407 mutex_unlock(&chip
->mutex
);
410 /* Figure out the number of words to write */
411 word_gap
= (-adr
& (map_bankwidth(map
)-1));
412 words
= (len
- word_gap
+ map_bankwidth(map
) - 1) / map_bankwidth(map
);
416 word_gap
= map_bankwidth(map
) - word_gap
;
418 datum
= map_word_ff(map
);
421 /* Get the program buffer offset from PFOW register data first*/
422 prog_buf_ofs
= map
->pfow_base
+ CMDVAL(map_read(map
,
423 map
->pfow_base
+ PFOW_PROGRAM_BUFFER_OFFSET
));
425 vec_seek
= *pvec_seek
;
427 int n
= map_bankwidth(map
) - word_gap
;
429 if (n
> vec
->iov_len
- vec_seek
)
430 n
= vec
->iov_len
- vec_seek
;
434 if (!word_gap
&& (len
< map_bankwidth(map
)))
435 datum
= map_word_ff(map
);
437 datum
= map_word_load_partial(map
, datum
,
438 vec
->iov_base
+ vec_seek
, word_gap
, n
);
442 if (!len
|| word_gap
== map_bankwidth(map
)) {
443 map_write(map
, datum
, prog_buf_ofs
);
444 prog_buf_ofs
+= map_bankwidth(map
);
449 if (vec_seek
== vec
->iov_len
) {
455 *pvec_seek
= vec_seek
;
458 send_pfow_command(map
, LPDDR_BUFF_PROGRAM
, adr
, wbufsize
, NULL
);
459 chip
->state
= FL_WRITING
;
460 ret
= wait_for_ready(map
, chip
, (1<<lpddr
->qinfo
->ProgBufferTime
));
462 printk(KERN_WARNING
"%s Buffer program error: %d at %lx; \n",
463 map
->name
, ret
, adr
);
467 out
: put_chip(map
, chip
);
468 mutex_unlock(&chip
->mutex
);
472 static int do_erase_oneblock(struct mtd_info
*mtd
, loff_t adr
)
474 struct map_info
*map
= mtd
->priv
;
475 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
476 int chipnum
= adr
>> lpddr
->chipshift
;
477 struct flchip
*chip
= &lpddr
->chips
[chipnum
];
480 mutex_lock(&chip
->mutex
);
481 ret
= get_chip(map
, chip
, FL_ERASING
);
483 mutex_unlock(&chip
->mutex
);
486 send_pfow_command(map
, LPDDR_BLOCK_ERASE
, adr
, 0, NULL
);
487 chip
->state
= FL_ERASING
;
488 ret
= wait_for_ready(map
, chip
, (1<<lpddr
->qinfo
->BlockEraseTime
)*1000);
490 printk(KERN_WARNING
"%s Erase block error %d at : %llx\n",
491 map
->name
, ret
, adr
);
494 out
: put_chip(map
, chip
);
495 mutex_unlock(&chip
->mutex
);
499 static int lpddr_read(struct mtd_info
*mtd
, loff_t adr
, size_t len
,
500 size_t *retlen
, u_char
*buf
)
502 struct map_info
*map
= mtd
->priv
;
503 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
504 int chipnum
= adr
>> lpddr
->chipshift
;
505 struct flchip
*chip
= &lpddr
->chips
[chipnum
];
508 mutex_lock(&chip
->mutex
);
509 ret
= get_chip(map
, chip
, FL_READY
);
511 mutex_unlock(&chip
->mutex
);
515 map_copy_from(map
, buf
, adr
, len
);
519 mutex_unlock(&chip
->mutex
);
523 static int lpddr_point(struct mtd_info
*mtd
, loff_t adr
, size_t len
,
524 size_t *retlen
, void **mtdbuf
, resource_size_t
*phys
)
526 struct map_info
*map
= mtd
->priv
;
527 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
528 int chipnum
= adr
>> lpddr
->chipshift
;
529 unsigned long ofs
, last_end
= 0;
530 struct flchip
*chip
= &lpddr
->chips
[chipnum
];
536 /* ofs: offset within the first chip that the first read should start */
537 ofs
= adr
- (chipnum
<< lpddr
->chipshift
);
538 *mtdbuf
= (void *)map
->virt
+ chip
->start
+ ofs
;
541 unsigned long thislen
;
543 if (chipnum
>= lpddr
->numchips
)
546 /* We cannot point across chips that are virtually disjoint */
548 last_end
= chip
->start
;
549 else if (chip
->start
!= last_end
)
552 if ((len
+ ofs
- 1) >> lpddr
->chipshift
)
553 thislen
= (1<<lpddr
->chipshift
) - ofs
;
557 mutex_lock(&chip
->mutex
);
558 ret
= get_chip(map
, chip
, FL_POINT
);
559 mutex_unlock(&chip
->mutex
);
563 chip
->state
= FL_POINT
;
564 chip
->ref_point_counter
++;
569 last_end
+= 1 << lpddr
->chipshift
;
571 chip
= &lpddr
->chips
[chipnum
];
576 static int lpddr_unpoint (struct mtd_info
*mtd
, loff_t adr
, size_t len
)
578 struct map_info
*map
= mtd
->priv
;
579 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
580 int chipnum
= adr
>> lpddr
->chipshift
, err
= 0;
583 /* ofs: offset within the first chip that the first read should start */
584 ofs
= adr
- (chipnum
<< lpddr
->chipshift
);
587 unsigned long thislen
;
590 chip
= &lpddr
->chips
[chipnum
];
591 if (chipnum
>= lpddr
->numchips
)
594 if ((len
+ ofs
- 1) >> lpddr
->chipshift
)
595 thislen
= (1<<lpddr
->chipshift
) - ofs
;
599 mutex_lock(&chip
->mutex
);
600 if (chip
->state
== FL_POINT
) {
601 chip
->ref_point_counter
--;
602 if (chip
->ref_point_counter
== 0)
603 chip
->state
= FL_READY
;
605 printk(KERN_WARNING
"%s: Warning: unpoint called on non"
606 "pointed region\n", map
->name
);
611 mutex_unlock(&chip
->mutex
);
621 static int lpddr_write_buffers(struct mtd_info
*mtd
, loff_t to
, size_t len
,
622 size_t *retlen
, const u_char
*buf
)
626 vec
.iov_base
= (void *) buf
;
629 return lpddr_writev(mtd
, &vec
, 1, to
, retlen
);
633 static int lpddr_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
634 unsigned long count
, loff_t to
, size_t *retlen
)
636 struct map_info
*map
= mtd
->priv
;
637 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
640 unsigned long ofs
, vec_seek
, i
;
641 int wbufsize
= 1 << lpddr
->qinfo
->BufSizeShift
;
644 for (i
= 0; i
< count
; i
++)
645 len
+= vecs
[i
].iov_len
;
650 chipnum
= to
>> lpddr
->chipshift
;
656 /* We must not cross write block boundaries */
657 int size
= wbufsize
- (ofs
& (wbufsize
-1));
662 ret
= do_write_buffer(map
, &lpddr
->chips
[chipnum
],
663 ofs
, &vecs
, &vec_seek
, size
);
671 /* Be nice and reschedule with the chip in a usable
672 * state for other processes */
680 static int lpddr_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
682 unsigned long ofs
, len
;
684 struct map_info
*map
= mtd
->priv
;
685 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
686 int size
= 1 << lpddr
->qinfo
->UniformBlockSizeShift
;
692 ret
= do_erase_oneblock(mtd
, ofs
);
698 instr
->state
= MTD_ERASE_DONE
;
699 mtd_erase_callback(instr
);
704 #define DO_XXLOCK_LOCK 1
705 #define DO_XXLOCK_UNLOCK 2
706 static int do_xxlock(struct mtd_info
*mtd
, loff_t adr
, uint32_t len
, int thunk
)
709 struct map_info
*map
= mtd
->priv
;
710 struct lpddr_private
*lpddr
= map
->fldrv_priv
;
711 int chipnum
= adr
>> lpddr
->chipshift
;
712 struct flchip
*chip
= &lpddr
->chips
[chipnum
];
714 mutex_lock(&chip
->mutex
);
715 ret
= get_chip(map
, chip
, FL_LOCKING
);
717 mutex_unlock(&chip
->mutex
);
721 if (thunk
== DO_XXLOCK_LOCK
) {
722 send_pfow_command(map
, LPDDR_LOCK_BLOCK
, adr
, adr
+ len
, NULL
);
723 chip
->state
= FL_LOCKING
;
724 } else if (thunk
== DO_XXLOCK_UNLOCK
) {
725 send_pfow_command(map
, LPDDR_UNLOCK_BLOCK
, adr
, adr
+ len
, NULL
);
726 chip
->state
= FL_UNLOCKING
;
730 ret
= wait_for_ready(map
, chip
, 1);
732 printk(KERN_ERR
"%s: block unlock error status %d \n",
736 out
: put_chip(map
, chip
);
737 mutex_unlock(&chip
->mutex
);
741 static int lpddr_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
743 return do_xxlock(mtd
, ofs
, len
, DO_XXLOCK_LOCK
);
746 static int lpddr_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
748 return do_xxlock(mtd
, ofs
, len
, DO_XXLOCK_UNLOCK
);
751 MODULE_LICENSE("GPL");
752 MODULE_AUTHOR("Alexey Korolev <akorolev@infradead.org>");
753 MODULE_DESCRIPTION("MTD driver for LPDDR flash chips");