1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for SEGA Dreamcast Visual Memory Unit
5 * Copyright (c) Adrian McMenamin 2002 - 2009
6 * Copyright (c) Paul Mundt 2001
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/sched.h>
11 #include <linux/delay.h>
12 #include <linux/maple.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/map.h>
17 unsigned char *buffer
; /* Cache */
18 unsigned int block
; /* Which block was cached */
19 unsigned long jiffies_atc
; /* When was it cached? */
24 struct maple_device
*mdev
;
33 struct vmu_cache
*pcache
;
46 unsigned char *blockread
;
47 struct vmupart
*parts
;
52 unsigned int num
; /* block number */
53 unsigned int ofs
; /* block offset */
56 static struct vmu_block
*ofs_to_block(unsigned long src_ofs
,
57 struct mtd_info
*mtd
, int partition
)
59 struct vmu_block
*vblock
;
60 struct maple_device
*mdev
;
62 struct mdev_part
*mpart
;
67 card
= maple_get_drvdata(mdev
);
69 if (src_ofs
>= card
->parts
[partition
].numblocks
* card
->blocklen
)
72 num
= src_ofs
/ card
->blocklen
;
73 if (num
> card
->parts
[partition
].numblocks
)
76 vblock
= kmalloc(sizeof(struct vmu_block
), GFP_KERNEL
);
81 vblock
->ofs
= src_ofs
% card
->blocklen
;
88 /* Maple bus callback function for reads */
89 static void vmu_blockread(struct mapleq
*mq
)
91 struct maple_device
*mdev
;
95 card
= maple_get_drvdata(mdev
);
96 /* copy the read in data */
98 if (unlikely(!card
->blockread
))
101 memcpy(card
->blockread
, mq
->recvbuf
->buf
+ 12,
102 card
->blocklen
/card
->readcnt
);
106 /* Interface with maple bus to read blocks
107 * caching the results so that other parts
108 * of the driver can access block reads */
109 static int maple_vmu_read_block(unsigned int num
, unsigned char *buf
,
110 struct mtd_info
*mtd
)
112 struct memcard
*card
;
113 struct mdev_part
*mpart
;
114 struct maple_device
*mdev
;
115 int partition
, error
= 0, x
, wait
;
116 unsigned char *blockread
= NULL
;
117 struct vmu_cache
*pcache
;
122 partition
= mpart
->partition
;
123 card
= maple_get_drvdata(mdev
);
124 pcache
= card
->parts
[partition
].pcache
;
127 /* prepare the cache for this block */
128 if (!pcache
->buffer
) {
129 pcache
->buffer
= kmalloc(card
->blocklen
, GFP_KERNEL
);
130 if (!pcache
->buffer
) {
131 dev_err(&mdev
->dev
, "VMU at (%d, %d) - read fails due"
132 " to lack of memory\n", mdev
->port
,
140 * Reads may be phased - again the hardware spec
141 * supports this - though may not be any devices in
142 * the wild that implement it, but we will here
144 for (x
= 0; x
< card
->readcnt
; x
++) {
145 sendbuf
= cpu_to_be32(partition
<< 24 | x
<< 16 | num
);
147 if (atomic_read(&mdev
->busy
) == 1) {
148 wait_event_interruptible_timeout(mdev
->maple_wait
,
149 atomic_read(&mdev
->busy
) == 0, HZ
);
150 if (atomic_read(&mdev
->busy
) == 1) {
151 dev_notice(&mdev
->dev
, "VMU at (%d, %d)"
152 " is busy\n", mdev
->port
, mdev
->unit
);
158 atomic_set(&mdev
->busy
, 1);
159 blockread
= kmalloc(card
->blocklen
/card
->readcnt
, GFP_KERNEL
);
162 atomic_set(&mdev
->busy
, 0);
165 card
->blockread
= blockread
;
167 maple_getcond_callback(mdev
, vmu_blockread
, 0,
169 error
= maple_add_packet(mdev
, MAPLE_FUNC_MEMCARD
,
170 MAPLE_COMMAND_BREAD
, 2, &sendbuf
);
171 /* Very long timeouts seem to be needed when box is stressed */
172 wait
= wait_event_interruptible_timeout(mdev
->maple_wait
,
173 (atomic_read(&mdev
->busy
) == 0 ||
174 atomic_read(&mdev
->busy
) == 2), HZ
* 3);
176 * MTD layer does not handle hotplugging well
177 * so have to return errors when VMU is unplugged
178 * in the middle of a read (busy == 2)
180 if (error
|| atomic_read(&mdev
->busy
) == 2) {
181 if (atomic_read(&mdev
->busy
) == 2)
183 atomic_set(&mdev
->busy
, 0);
184 card
->blockread
= NULL
;
187 if (wait
== 0 || wait
== -ERESTARTSYS
) {
188 card
->blockread
= NULL
;
189 atomic_set(&mdev
->busy
, 0);
191 list_del_init(&(mdev
->mq
->list
));
192 kfree(mdev
->mq
->sendbuf
);
193 mdev
->mq
->sendbuf
= NULL
;
194 if (wait
== -ERESTARTSYS
) {
195 dev_warn(&mdev
->dev
, "VMU read on (%d, %d)"
196 " interrupted on block 0x%X\n",
197 mdev
->port
, mdev
->unit
, num
);
199 dev_notice(&mdev
->dev
, "VMU read on (%d, %d)"
200 " timed out on block 0x%X\n",
201 mdev
->port
, mdev
->unit
, num
);
205 memcpy(buf
+ (card
->blocklen
/card
->readcnt
) * x
, blockread
,
206 card
->blocklen
/card
->readcnt
);
208 memcpy(pcache
->buffer
+ (card
->blocklen
/card
->readcnt
) * x
,
209 card
->blockread
, card
->blocklen
/card
->readcnt
);
210 card
->blockread
= NULL
;
212 pcache
->jiffies_atc
= jiffies
;
225 /* communicate with maple bus for phased writing */
226 static int maple_vmu_write_block(unsigned int num
, const unsigned char *buf
,
227 struct mtd_info
*mtd
)
229 struct memcard
*card
;
230 struct mdev_part
*mpart
;
231 struct maple_device
*mdev
;
232 int partition
, error
, locking
, x
, phaselen
, wait
;
237 partition
= mpart
->partition
;
238 card
= maple_get_drvdata(mdev
);
240 phaselen
= card
->blocklen
/card
->writecnt
;
242 sendbuf
= kmalloc(phaselen
+ 4, GFP_KERNEL
);
247 for (x
= 0; x
< card
->writecnt
; x
++) {
248 sendbuf
[0] = cpu_to_be32(partition
<< 24 | x
<< 16 | num
);
249 memcpy(&sendbuf
[1], buf
+ phaselen
* x
, phaselen
);
250 /* wait until the device is not busy doing something else
251 * or 1 second - which ever is longer */
252 if (atomic_read(&mdev
->busy
) == 1) {
253 wait_event_interruptible_timeout(mdev
->maple_wait
,
254 atomic_read(&mdev
->busy
) == 0, HZ
);
255 if (atomic_read(&mdev
->busy
) == 1) {
257 dev_notice(&mdev
->dev
, "VMU write at (%d, %d)"
258 "failed - device is busy\n",
259 mdev
->port
, mdev
->unit
);
263 atomic_set(&mdev
->busy
, 1);
265 locking
= maple_add_packet(mdev
, MAPLE_FUNC_MEMCARD
,
266 MAPLE_COMMAND_BWRITE
, phaselen
/ 4 + 2, sendbuf
);
267 wait
= wait_event_interruptible_timeout(mdev
->maple_wait
,
268 atomic_read(&mdev
->busy
) == 0, HZ
/10);
271 atomic_set(&mdev
->busy
, 0);
274 if (atomic_read(&mdev
->busy
) == 2) {
275 atomic_set(&mdev
->busy
, 0);
276 } else if (wait
== 0 || wait
== -ERESTARTSYS
) {
278 dev_warn(&mdev
->dev
, "Write at (%d, %d) of block"
279 " 0x%X at phase %d failed: could not"
280 " communicate with VMU", mdev
->port
,
282 atomic_set(&mdev
->busy
, 0);
283 kfree(mdev
->mq
->sendbuf
);
284 mdev
->mq
->sendbuf
= NULL
;
285 list_del_init(&(mdev
->mq
->list
));
291 return card
->blocklen
;
296 dev_err(&mdev
->dev
, "VMU (%d, %d): write failed\n", mdev
->port
,
301 /* mtd function to simulate reading byte by byte */
302 static unsigned char vmu_flash_read_char(unsigned long ofs
, int *retval
,
303 struct mtd_info
*mtd
)
305 struct vmu_block
*vblock
;
306 struct memcard
*card
;
307 struct mdev_part
*mpart
;
308 struct maple_device
*mdev
;
309 unsigned char *buf
, ret
;
310 int partition
, error
;
314 partition
= mpart
->partition
;
315 card
= maple_get_drvdata(mdev
);
318 buf
= kmalloc(card
->blocklen
, GFP_KERNEL
);
325 vblock
= ofs_to_block(ofs
, mtd
, partition
);
332 error
= maple_vmu_read_block(vblock
->num
, buf
, mtd
);
339 ret
= buf
[vblock
->ofs
];
349 /* mtd higher order function to read flash */
350 static int vmu_flash_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
351 size_t *retlen
, u_char
*buf
)
353 struct maple_device
*mdev
;
354 struct memcard
*card
;
355 struct mdev_part
*mpart
;
356 struct vmu_cache
*pcache
;
357 struct vmu_block
*vblock
;
358 int index
= 0, retval
, partition
, leftover
, numblocks
;
363 partition
= mpart
->partition
;
364 card
= maple_get_drvdata(mdev
);
366 numblocks
= card
->parts
[partition
].numblocks
;
367 if (from
+ len
> numblocks
* card
->blocklen
)
368 len
= numblocks
* card
->blocklen
- from
;
371 /* Have we cached this bit already? */
372 pcache
= card
->parts
[partition
].pcache
;
374 vblock
= ofs_to_block(from
+ index
, mtd
, partition
);
377 /* Have we cached this and is the cache valid and timely? */
379 time_before(jiffies
, pcache
->jiffies_atc
+ HZ
) &&
380 (pcache
->block
== vblock
->num
)) {
381 /* we have cached it, so do necessary copying */
382 leftover
= card
->blocklen
- vblock
->ofs
;
383 if (vblock
->ofs
+ len
- index
< card
->blocklen
) {
384 /* only a bit of this block to copy */
386 pcache
->buffer
+ vblock
->ofs
,
390 /* otherwise copy remainder of whole block */
391 memcpy(buf
+ index
, pcache
->buffer
+
392 vblock
->ofs
, leftover
);
397 * Not cached so read one byte -
398 * but cache the rest of the block
400 cx
= vmu_flash_read_char(from
+ index
, &retval
, mtd
);
406 memset(buf
+ index
, cx
, 1);
410 } while (len
> index
);
416 static int vmu_flash_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
417 size_t *retlen
, const u_char
*buf
)
419 struct maple_device
*mdev
;
420 struct memcard
*card
;
421 struct mdev_part
*mpart
;
422 int index
= 0, partition
, error
= 0, numblocks
;
423 struct vmu_cache
*pcache
;
424 struct vmu_block
*vblock
;
425 unsigned char *buffer
;
429 partition
= mpart
->partition
;
430 card
= maple_get_drvdata(mdev
);
432 numblocks
= card
->parts
[partition
].numblocks
;
433 if (to
+ len
> numblocks
* card
->blocklen
)
434 len
= numblocks
* card
->blocklen
- to
;
440 vblock
= ofs_to_block(to
, mtd
, partition
);
446 buffer
= kmalloc(card
->blocklen
, GFP_KERNEL
);
453 /* Read in the block we are to write to */
454 error
= maple_vmu_read_block(vblock
->num
, buffer
, mtd
);
459 buffer
[vblock
->ofs
] = buf
[index
];
464 } while (vblock
->ofs
< card
->blocklen
);
466 /* write out new buffer */
467 error
= maple_vmu_write_block(vblock
->num
, buffer
, mtd
);
468 /* invalidate the cache */
469 pcache
= card
->parts
[partition
].pcache
;
472 if (error
!= card
->blocklen
)
477 } while (len
> index
);
489 dev_err(&mdev
->dev
, "VMU write failing with error %d\n", error
);
493 static void vmu_flash_sync(struct mtd_info
*mtd
)
495 /* Do nothing here */
498 /* Maple bus callback function to recursively query hardware details */
499 static void vmu_queryblocks(struct mapleq
*mq
)
501 struct maple_device
*mdev
;
503 struct memcard
*card
;
505 struct vmu_cache
*pcache
;
506 struct mdev_part
*mpart
;
507 struct mtd_info
*mtd_cur
;
508 struct vmupart
*part_cur
;
512 card
= maple_get_drvdata(mdev
);
513 res
= (unsigned short *) (mq
->recvbuf
->buf
);
514 card
->tempA
= res
[12];
515 card
->tempB
= res
[6];
517 dev_info(&mdev
->dev
, "VMU device at partition %d has %d user "
518 "blocks with a root block at %d\n", card
->partition
,
519 card
->tempA
, card
->tempB
);
521 part_cur
= &card
->parts
[card
->partition
];
522 part_cur
->user_blocks
= card
->tempA
;
523 part_cur
->root_block
= card
->tempB
;
524 part_cur
->numblocks
= card
->tempB
+ 1;
525 part_cur
->name
= kmalloc(12, GFP_KERNEL
);
529 sprintf(part_cur
->name
, "vmu%d.%d.%d",
530 mdev
->port
, mdev
->unit
, card
->partition
);
531 mtd_cur
= &card
->mtd
[card
->partition
];
532 mtd_cur
->name
= part_cur
->name
;
534 mtd_cur
->flags
= MTD_WRITEABLE
|MTD_NO_ERASE
;
535 mtd_cur
->size
= part_cur
->numblocks
* card
->blocklen
;
536 mtd_cur
->erasesize
= card
->blocklen
;
537 mtd_cur
->_write
= vmu_flash_write
;
538 mtd_cur
->_read
= vmu_flash_read
;
539 mtd_cur
->_sync
= vmu_flash_sync
;
540 mtd_cur
->writesize
= card
->blocklen
;
542 mpart
= kmalloc(sizeof(struct mdev_part
), GFP_KERNEL
);
547 mpart
->partition
= card
->partition
;
548 mtd_cur
->priv
= mpart
;
549 mtd_cur
->owner
= THIS_MODULE
;
551 pcache
= kzalloc(sizeof(struct vmu_cache
), GFP_KERNEL
);
553 goto fail_cache_create
;
554 part_cur
->pcache
= pcache
;
556 error
= mtd_device_register(mtd_cur
, NULL
, 0);
558 goto fail_mtd_register
;
560 maple_getcond_callback(mdev
, NULL
, 0,
564 * Set up a recursive call to the (probably theoretical)
565 * second or more partition
567 if (++card
->partition
< card
->partitions
) {
568 partnum
= cpu_to_be32(card
->partition
<< 24);
569 maple_getcond_callback(mdev
, vmu_queryblocks
, 0,
571 maple_add_packet(mdev
, MAPLE_FUNC_MEMCARD
,
572 MAPLE_COMMAND_GETMINFO
, 2, &partnum
);
577 dev_err(&mdev
->dev
, "Could not register maple device at (%d, %d)"
578 "error is 0x%X\n", mdev
->port
, mdev
->unit
, error
);
579 for (error
= 0; error
<= card
->partition
; error
++) {
580 kfree(((card
->parts
)[error
]).pcache
);
581 ((card
->parts
)[error
]).pcache
= NULL
;
585 for (error
= 0; error
<= card
->partition
; error
++) {
586 kfree(((card
->mtd
)[error
]).priv
);
587 ((card
->mtd
)[error
]).priv
= NULL
;
589 maple_getcond_callback(mdev
, NULL
, 0,
591 kfree(part_cur
->name
);
596 /* Handles very basic info about the flash, queries for details */
597 static int vmu_connect(struct maple_device
*mdev
)
599 unsigned long test_flash_data
, basic_flash_data
;
601 struct memcard
*card
;
604 test_flash_data
= be32_to_cpu(mdev
->devinfo
.function
);
605 /* Need to count how many bits are set - to find out which
606 * function_data element has details of the memory card
608 c
= hweight_long(test_flash_data
);
610 basic_flash_data
= be32_to_cpu(mdev
->devinfo
.function_data
[c
- 1]);
612 card
= kmalloc(sizeof(struct memcard
), GFP_KERNEL
);
618 card
->partitions
= (basic_flash_data
>> 24 & 0xFF) + 1;
619 card
->blocklen
= ((basic_flash_data
>> 16 & 0xFF) + 1) << 5;
620 card
->writecnt
= basic_flash_data
>> 12 & 0xF;
621 card
->readcnt
= basic_flash_data
>> 8 & 0xF;
622 card
->removable
= basic_flash_data
>> 7 & 1;
627 * Not sure there are actually any multi-partition devices in the
628 * real world, but the hardware supports them, so, so will we
630 card
->parts
= kmalloc_array(card
->partitions
, sizeof(struct vmupart
),
634 goto fail_partitions
;
637 card
->mtd
= kmalloc_array(card
->partitions
, sizeof(struct mtd_info
),
644 maple_set_drvdata(mdev
, card
);
647 * We want to trap meminfo not get cond
648 * so set interval to zero, but rely on maple bus
649 * driver to pass back the results of the meminfo
651 maple_getcond_callback(mdev
, vmu_queryblocks
, 0,
654 /* Make sure we are clear to go */
655 if (atomic_read(&mdev
->busy
) == 1) {
656 wait_event_interruptible_timeout(mdev
->maple_wait
,
657 atomic_read(&mdev
->busy
) == 0, HZ
);
658 if (atomic_read(&mdev
->busy
) == 1) {
659 dev_notice(&mdev
->dev
, "VMU at (%d, %d) is busy\n",
660 mdev
->port
, mdev
->unit
);
662 goto fail_device_busy
;
666 atomic_set(&mdev
->busy
, 1);
669 * Set up the minfo call: vmu_queryblocks will handle
670 * the information passed back
672 error
= maple_add_packet(mdev
, MAPLE_FUNC_MEMCARD
,
673 MAPLE_COMMAND_GETMINFO
, 2, &partnum
);
675 dev_err(&mdev
->dev
, "Could not lock VMU at (%d, %d)"
676 " error is 0x%X\n", mdev
->port
, mdev
->unit
, error
);
691 static void vmu_disconnect(struct maple_device
*mdev
)
693 struct memcard
*card
;
694 struct mdev_part
*mpart
;
697 mdev
->callback
= NULL
;
698 card
= maple_get_drvdata(mdev
);
699 for (x
= 0; x
< card
->partitions
; x
++) {
700 mpart
= ((card
->mtd
)[x
]).priv
;
702 mtd_device_unregister(&((card
->mtd
)[x
]));
703 kfree(((card
->parts
)[x
]).name
);
710 /* Callback to handle eccentricities of both mtd subsystem
711 * and general flakyness of Dreamcast VMUs
713 static int vmu_can_unload(struct maple_device
*mdev
)
715 struct memcard
*card
;
717 struct mtd_info
*mtd
;
719 card
= maple_get_drvdata(mdev
);
720 for (x
= 0; x
< card
->partitions
; x
++) {
721 mtd
= &((card
->mtd
)[x
]);
722 if (mtd
->usecount
> 0)
728 #define ERRSTR "VMU at (%d, %d) file error -"
730 static void vmu_file_error(struct maple_device
*mdev
, void *recvbuf
)
732 enum maple_file_errors error
= ((int *)recvbuf
)[1];
736 case MAPLE_FILEERR_INVALID_PARTITION
:
737 dev_notice(&mdev
->dev
, ERRSTR
" invalid partition number\n",
738 mdev
->port
, mdev
->unit
);
741 case MAPLE_FILEERR_PHASE_ERROR
:
742 dev_notice(&mdev
->dev
, ERRSTR
" phase error\n",
743 mdev
->port
, mdev
->unit
);
746 case MAPLE_FILEERR_INVALID_BLOCK
:
747 dev_notice(&mdev
->dev
, ERRSTR
" invalid block number\n",
748 mdev
->port
, mdev
->unit
);
751 case MAPLE_FILEERR_WRITE_ERROR
:
752 dev_notice(&mdev
->dev
, ERRSTR
" write error\n",
753 mdev
->port
, mdev
->unit
);
756 case MAPLE_FILEERR_INVALID_WRITE_LENGTH
:
757 dev_notice(&mdev
->dev
, ERRSTR
" invalid write length\n",
758 mdev
->port
, mdev
->unit
);
761 case MAPLE_FILEERR_BAD_CRC
:
762 dev_notice(&mdev
->dev
, ERRSTR
" bad CRC\n",
763 mdev
->port
, mdev
->unit
);
767 dev_notice(&mdev
->dev
, ERRSTR
" 0x%X\n",
768 mdev
->port
, mdev
->unit
, error
);
773 static int probe_maple_vmu(struct device
*dev
)
775 struct maple_device
*mdev
= to_maple_dev(dev
);
776 struct maple_driver
*mdrv
= to_maple_driver(dev
->driver
);
778 mdev
->can_unload
= vmu_can_unload
;
779 mdev
->fileerr_handler
= vmu_file_error
;
782 return vmu_connect(mdev
);
785 static int remove_maple_vmu(struct device
*dev
)
787 struct maple_device
*mdev
= to_maple_dev(dev
);
789 vmu_disconnect(mdev
);
793 static struct maple_driver vmu_flash_driver
= {
794 .function
= MAPLE_FUNC_MEMCARD
,
796 .name
= "Dreamcast_visual_memory",
797 .probe
= probe_maple_vmu
,
798 .remove
= remove_maple_vmu
,
802 static int __init
vmu_flash_map_init(void)
804 return maple_driver_register(&vmu_flash_driver
);
807 static void __exit
vmu_flash_map_exit(void)
809 maple_driver_unregister(&vmu_flash_driver
);
812 module_init(vmu_flash_map_init
);
813 module_exit(vmu_flash_map_exit
);
815 MODULE_LICENSE("GPL");
816 MODULE_AUTHOR("Adrian McMenamin");
817 MODULE_DESCRIPTION("Flash mapping for Sega Dreamcast visual memory");