Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Support for the N64 cart. |
| 4 | * |
| 5 | * Copyright (c) 2021 Lauri Kasanen |
| 6 | */ |
| 7 | |
Chaitanya Kulkarni | f1e1922 | 2021-01-25 15:32:35 -0800 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 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> |
| 15 | |
Chaitanya Kulkarni | 2ce503b | 2021-01-25 15:32:38 -0800 | [diff] [blame^] | 16 | enum { |
| 17 | PI_DRAM_REG = 0, |
| 18 | PI_CART_REG, |
| 19 | PI_READ_REG, |
| 20 | PI_WRITE_REG, |
| 21 | PI_STATUS_REG, |
| 22 | }; |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 23 | |
| 24 | #define PI_STATUS_DMA_BUSY (1 << 0) |
| 25 | #define PI_STATUS_IO_BUSY (1 << 1) |
| 26 | |
| 27 | #define CART_DOMAIN 0x10000000 |
| 28 | #define CART_MAX 0x1FFFFFFF |
| 29 | |
| 30 | #define MIN_ALIGNMENT 8 |
| 31 | |
Chaitanya Kulkarni | e39e313 | 2021-01-25 15:32:37 -0800 | [diff] [blame] | 32 | static u32 __iomem *reg_base; |
| 33 | static struct device *dev; |
| 34 | |
| 35 | static unsigned int start; |
| 36 | module_param(start, uint, 0); |
| 37 | MODULE_PARM_DESC(start, "Start address of the cart block data"); |
| 38 | |
| 39 | static unsigned int size; |
| 40 | module_param(size, uint, 0); |
| 41 | MODULE_PARM_DESC(size, "Size of the cart block data, in bytes"); |
| 42 | |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 43 | static void n64cart_write_reg(const u8 reg, const u32 value) |
| 44 | { |
| 45 | writel(value, reg_base + reg); |
| 46 | } |
| 47 | |
| 48 | static u32 n64cart_read_reg(const u8 reg) |
| 49 | { |
| 50 | return readl(reg_base + reg); |
| 51 | } |
| 52 | |
| 53 | static void n64cart_wait_dma(void) |
| 54 | { |
| 55 | while (n64cart_read_reg(PI_STATUS_REG) & |
| 56 | (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY)) |
| 57 | cpu_relax(); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Process a single bvec of a bio. |
| 62 | */ |
| 63 | static bool n64cart_do_bvec(struct device *dev, struct bio_vec *bv, u32 pos) |
| 64 | { |
| 65 | dma_addr_t dma_addr; |
| 66 | const u32 bstart = pos + start; |
| 67 | |
| 68 | /* Alignment check */ |
| 69 | WARN_ON_ONCE((bv->bv_offset & (MIN_ALIGNMENT - 1)) || |
| 70 | (bv->bv_len & (MIN_ALIGNMENT - 1))); |
| 71 | |
| 72 | dma_addr = dma_map_bvec(dev, bv, DMA_FROM_DEVICE, 0); |
| 73 | if (dma_mapping_error(dev, dma_addr)) |
| 74 | return false; |
| 75 | |
| 76 | n64cart_wait_dma(); |
| 77 | |
| 78 | n64cart_write_reg(PI_DRAM_REG, dma_addr + bv->bv_offset); |
| 79 | n64cart_write_reg(PI_CART_REG, (bstart | CART_DOMAIN) & CART_MAX); |
| 80 | n64cart_write_reg(PI_WRITE_REG, bv->bv_len - 1); |
| 81 | |
| 82 | n64cart_wait_dma(); |
| 83 | |
| 84 | dma_unmap_page(dev, dma_addr, bv->bv_len, DMA_FROM_DEVICE); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | static blk_qc_t n64cart_submit_bio(struct bio *bio) |
| 89 | { |
| 90 | struct bio_vec bvec; |
| 91 | u32 pos; |
| 92 | struct bvec_iter iter; |
| 93 | |
| 94 | pos = bio->bi_iter.bi_sector << SECTOR_SHIFT; |
| 95 | |
| 96 | bio_for_each_segment(bvec, bio, iter) { |
| 97 | if (!n64cart_do_bvec(dev, &bvec, pos)) |
| 98 | goto io_error; |
| 99 | pos += bvec.bv_len; |
| 100 | } |
| 101 | |
| 102 | bio_endio(bio); |
| 103 | return BLK_QC_T_NONE; |
| 104 | io_error: |
| 105 | bio_io_error(bio); |
| 106 | return BLK_QC_T_NONE; |
| 107 | } |
| 108 | |
| 109 | static const struct block_device_operations n64cart_fops = { |
| 110 | .owner = THIS_MODULE, |
| 111 | .submit_bio = n64cart_submit_bio, |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * The target device is embedded and RAM-constrained. We save RAM |
| 116 | * by initializing in __init code that gets dropped late in boot. |
| 117 | * For the same reason there is no module or unloading support. |
| 118 | */ |
| 119 | static int __init n64cart_probe(struct platform_device *pdev) |
| 120 | { |
| 121 | int err; |
| 122 | struct request_queue *queue; |
| 123 | struct gendisk *disk; |
| 124 | |
| 125 | if (!start || !size) { |
Chaitanya Kulkarni | f1e1922 | 2021-01-25 15:32:35 -0800 | [diff] [blame] | 126 | pr_err("start or size not specified\n"); |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 127 | return -ENODEV; |
| 128 | } |
| 129 | |
| 130 | if (size & 4095) { |
Chaitanya Kulkarni | f1e1922 | 2021-01-25 15:32:35 -0800 | [diff] [blame] | 131 | pr_err("size must be a multiple of 4K\n"); |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 132 | return -ENODEV; |
| 133 | } |
| 134 | |
| 135 | queue = blk_alloc_queue(NUMA_NO_NODE); |
| 136 | if (!queue) { |
| 137 | return -ENOMEM; |
| 138 | } |
| 139 | |
| 140 | reg_base = devm_platform_ioremap_resource(pdev, 0); |
| 141 | if (!reg_base) { |
| 142 | err = -EINVAL; |
| 143 | goto fail_queue; |
| 144 | } |
| 145 | |
| 146 | disk = alloc_disk(0); |
| 147 | if (!disk) { |
| 148 | err = -ENOMEM; |
| 149 | goto fail_queue; |
| 150 | } |
| 151 | |
| 152 | dev = &pdev->dev; |
| 153 | |
| 154 | disk->first_minor = 0; |
| 155 | disk->queue = queue; |
| 156 | disk->flags = GENHD_FL_NO_PART_SCAN | GENHD_FL_EXT_DEVT; |
| 157 | disk->fops = &n64cart_fops; |
| 158 | strcpy(disk->disk_name, "n64cart"); |
| 159 | |
| 160 | set_capacity(disk, size / 512); |
| 161 | set_disk_ro(disk, 1); |
| 162 | |
| 163 | blk_queue_flag_set(QUEUE_FLAG_NONROT, queue); |
| 164 | blk_queue_physical_block_size(queue, 4096); |
| 165 | blk_queue_logical_block_size(queue, 4096); |
| 166 | |
| 167 | add_disk(disk); |
| 168 | |
| 169 | pr_info("n64cart: %u kb disk\n", size / 1024); |
| 170 | |
| 171 | return 0; |
| 172 | fail_queue: |
| 173 | blk_cleanup_queue(queue); |
| 174 | |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | static struct platform_driver n64cart_driver = { |
| 179 | .driver = { |
| 180 | .name = "n64cart", |
| 181 | }, |
| 182 | }; |
| 183 | |
| 184 | static int __init n64cart_init(void) |
| 185 | { |
| 186 | return platform_driver_probe(&n64cart_driver, n64cart_probe); |
| 187 | } |
| 188 | |
Lauri Kasanen | d9b2a2b | 2021-01-23 09:53:27 +0200 | [diff] [blame] | 189 | module_init(n64cart_init); |
Chaitanya Kulkarni | 9ee8c9a | 2021-01-25 15:32:36 -0800 | [diff] [blame] | 190 | |
| 191 | MODULE_AUTHOR("Lauri Kasanen <cand@gmx.com>"); |
| 192 | MODULE_DESCRIPTION("Driver for the N64 cart"); |
| 193 | MODULE_LICENSE("GPL"); |