1 // SPDX-License-Identifier: GPL-2.0
3 * Support for the N64 cart.
5 * Copyright (c) 2021 Lauri Kasanen
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/bitops.h>
10 #include <linux/blkdev.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
24 #define PI_STATUS_DMA_BUSY (1 << 0)
25 #define PI_STATUS_IO_BUSY (1 << 1)
27 #define CART_DOMAIN 0x10000000
28 #define CART_MAX 0x1FFFFFFF
30 #define MIN_ALIGNMENT 8
32 static u32 __iomem *reg_base;
34 static unsigned int start;
35 module_param(start, uint, 0);
36 MODULE_PARM_DESC(start, "Start address of the cart block data");
38 static unsigned int size;
39 module_param(size, uint, 0);
40 MODULE_PARM_DESC(size, "Size of the cart block data, in bytes");
42 static void n64cart_write_reg(const u8 reg, const u32 value)
44 writel(value, reg_base + reg);
47 static u32 n64cart_read_reg(const u8 reg)
49 return readl(reg_base + reg);
52 static void n64cart_wait_dma(void)
54 while (n64cart_read_reg(PI_STATUS_REG) &
55 (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY))
60 * Process a single bvec of a bio.
62 static bool n64cart_do_bvec(struct device *dev, struct bio_vec *bv, u32 pos)
65 const u32 bstart = pos + start;
68 WARN_ON_ONCE((bv->bv_offset & (MIN_ALIGNMENT - 1)) ||
69 (bv->bv_len & (MIN_ALIGNMENT - 1)));
71 dma_addr = dma_map_bvec(dev, bv, DMA_FROM_DEVICE, 0);
72 if (dma_mapping_error(dev, dma_addr))
77 n64cart_write_reg(PI_DRAM_REG, dma_addr);
78 n64cart_write_reg(PI_CART_REG, (bstart | CART_DOMAIN) & CART_MAX);
79 n64cart_write_reg(PI_WRITE_REG, bv->bv_len - 1);
83 dma_unmap_page(dev, dma_addr, bv->bv_len, DMA_FROM_DEVICE);
87 static blk_qc_t n64cart_submit_bio(struct bio *bio)
90 struct bvec_iter iter;
91 struct device *dev = bio->bi_bdev->bd_disk->private_data;
92 u32 pos = bio->bi_iter.bi_sector << SECTOR_SHIFT;
94 bio_for_each_segment(bvec, bio, iter) {
95 if (!n64cart_do_bvec(dev, &bvec, pos))
101 return BLK_QC_T_NONE;
104 return BLK_QC_T_NONE;
107 static const struct block_device_operations n64cart_fops = {
108 .owner = THIS_MODULE,
109 .submit_bio = n64cart_submit_bio,
113 * The target device is embedded and RAM-constrained. We save RAM
114 * by initializing in __init code that gets dropped late in boot.
115 * For the same reason there is no module or unloading support.
117 static int __init n64cart_probe(struct platform_device *pdev)
119 struct gendisk *disk;
121 if (!start || !size) {
122 pr_err("start or size not specified\n");
127 pr_err("size must be a multiple of 4K\n");
131 reg_base = devm_platform_ioremap_resource(pdev, 0);
132 if (IS_ERR(reg_base))
133 return PTR_ERR(reg_base);
135 disk = blk_alloc_disk(NUMA_NO_NODE);
139 disk->first_minor = 0;
140 disk->flags = GENHD_FL_NO_PART_SCAN;
141 disk->fops = &n64cart_fops;
142 disk->private_data = &pdev->dev;
143 strcpy(disk->disk_name, "n64cart");
145 set_capacity(disk, size >> SECTOR_SHIFT);
146 set_disk_ro(disk, 1);
148 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
149 blk_queue_physical_block_size(disk->queue, 4096);
150 blk_queue_logical_block_size(disk->queue, 4096);
154 pr_info("n64cart: %u kb disk\n", size / 1024);
159 static struct platform_driver n64cart_driver = {
165 static int __init n64cart_init(void)
167 return platform_driver_probe(&n64cart_driver, n64cart_probe);
170 module_init(n64cart_init);
172 MODULE_AUTHOR("Lauri Kasanen <cand@gmx.com>");
173 MODULE_DESCRIPTION("Driver for the N64 cart");
174 MODULE_LICENSE("GPL");