Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 2 | /* jazz_esp.c: ESP front-end for MIPS JAZZ systems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 4 | * Copyright (C) 2007 Thomas Bogendörfer (tsbogend@alpha.frankende) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/irq.h> |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/dma.h> |
| 19 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 20 | #include <asm/jazz.h> |
| 21 | #include <asm/jazzdma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 23 | #include <scsi/scsi_host.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 25 | #include "esp_scsi.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 27 | #define DRV_MODULE_NAME "jazz_esp" |
| 28 | #define PFX DRV_MODULE_NAME ": " |
| 29 | #define DRV_VERSION "1.000" |
| 30 | #define DRV_MODULE_RELDATE "May 19, 2007" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 32 | static void jazz_esp_write8(struct esp *esp, u8 val, unsigned long reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 34 | *(volatile u8 *)(esp->regs + reg) = val; |
| 35 | } |
| 36 | |
| 37 | static u8 jazz_esp_read8(struct esp *esp, unsigned long reg) |
| 38 | { |
| 39 | return *(volatile u8 *)(esp->regs + reg); |
| 40 | } |
| 41 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 42 | static int jazz_esp_irq_pending(struct esp *esp) |
| 43 | { |
| 44 | if (jazz_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR) |
| 45 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 49 | static void jazz_esp_reset_dma(struct esp *esp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 51 | vdma_disable ((int)esp->dma_regs); |
| 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 54 | static void jazz_esp_dma_drain(struct esp *esp) |
| 55 | { |
| 56 | /* nothing to do */ |
| 57 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 59 | static void jazz_esp_dma_invalidate(struct esp *esp) |
| 60 | { |
| 61 | vdma_disable ((int)esp->dma_regs); |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 64 | static void jazz_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count, |
| 65 | u32 dma_count, int write, u8 cmd) |
| 66 | { |
| 67 | BUG_ON(!(cmd & ESP_CMD_DMA)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 69 | jazz_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW); |
| 70 | jazz_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED); |
| 71 | vdma_disable ((int)esp->dma_regs); |
| 72 | if (write) |
| 73 | vdma_set_mode ((int)esp->dma_regs, DMA_MODE_READ); |
| 74 | else |
| 75 | vdma_set_mode ((int)esp->dma_regs, DMA_MODE_WRITE); |
| 76 | |
| 77 | vdma_set_addr ((int)esp->dma_regs, addr); |
| 78 | vdma_set_count ((int)esp->dma_regs, dma_count); |
| 79 | vdma_enable ((int)esp->dma_regs); |
| 80 | |
| 81 | scsi_esp_cmd(esp, cmd); |
| 82 | } |
| 83 | |
| 84 | static int jazz_esp_dma_error(struct esp *esp) |
| 85 | { |
| 86 | u32 enable = vdma_get_enable((int)esp->dma_regs); |
| 87 | |
| 88 | if (enable & (R4030_MEM_INTR|R4030_ADDR_INTR)) |
| 89 | return 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static const struct esp_driver_ops jazz_esp_ops = { |
| 95 | .esp_write8 = jazz_esp_write8, |
| 96 | .esp_read8 = jazz_esp_read8, |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 97 | .irq_pending = jazz_esp_irq_pending, |
| 98 | .reset_dma = jazz_esp_reset_dma, |
| 99 | .dma_drain = jazz_esp_dma_drain, |
| 100 | .dma_invalidate = jazz_esp_dma_invalidate, |
| 101 | .send_dma_cmd = jazz_esp_send_dma_cmd, |
| 102 | .dma_error = jazz_esp_dma_error, |
| 103 | }; |
| 104 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 105 | static int esp_jazz_probe(struct platform_device *dev) |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 106 | { |
| 107 | struct scsi_host_template *tpnt = &scsi_esp_template; |
| 108 | struct Scsi_Host *host; |
| 109 | struct esp *esp; |
| 110 | struct resource *res; |
| 111 | int err; |
| 112 | |
| 113 | host = scsi_host_alloc(tpnt, sizeof(struct esp)); |
| 114 | |
| 115 | err = -ENOMEM; |
| 116 | if (!host) |
| 117 | goto fail; |
| 118 | |
| 119 | host->max_id = 8; |
Christoph Hellwig | 2b14ec7 | 2007-05-31 20:12:32 +0200 | [diff] [blame] | 120 | esp = shost_priv(host); |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 121 | |
| 122 | esp->host = host; |
Finn Thain | e32ec65 | 2018-03-07 17:56:41 +1100 | [diff] [blame] | 123 | esp->dev = &dev->dev; |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 124 | esp->ops = &jazz_esp_ops; |
| 125 | |
| 126 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 127 | if (!res) |
| 128 | goto fail_unlink; |
| 129 | |
| 130 | esp->regs = (void __iomem *)res->start; |
| 131 | if (!esp->regs) |
| 132 | goto fail_unlink; |
| 133 | |
| 134 | res = platform_get_resource(dev, IORESOURCE_MEM, 1); |
| 135 | if (!res) |
| 136 | goto fail_unlink; |
| 137 | |
| 138 | esp->dma_regs = (void __iomem *)res->start; |
| 139 | |
| 140 | esp->command_block = dma_alloc_coherent(esp->dev, 16, |
| 141 | &esp->command_block_dma, |
| 142 | GFP_KERNEL); |
| 143 | if (!esp->command_block) |
| 144 | goto fail_unmap_regs; |
| 145 | |
Sergey Shtylyov | 38fca15 | 2021-03-30 20:43:23 +0300 | [diff] [blame] | 146 | host->irq = err = platform_get_irq(dev, 0); |
| 147 | if (err < 0) |
| 148 | goto fail_unmap_command_block; |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 149 | err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp); |
| 150 | if (err < 0) |
| 151 | goto fail_unmap_command_block; |
| 152 | |
| 153 | esp->scsi_id = 7; |
| 154 | esp->host->this_id = esp->scsi_id; |
| 155 | esp->scsi_id_mask = (1 << esp->scsi_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | esp->cfreq = 40000000; |
| 157 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 158 | dev_set_drvdata(&dev->dev, esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Christoph Hellwig | 44b1b4d | 2018-10-13 09:26:26 +0200 | [diff] [blame] | 160 | err = scsi_esp_register(esp); |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 161 | if (err) |
| 162 | goto fail_free_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 164 | return 0; |
| 165 | |
| 166 | fail_free_irq: |
| 167 | free_irq(host->irq, esp); |
| 168 | fail_unmap_command_block: |
| 169 | dma_free_coherent(esp->dev, 16, |
| 170 | esp->command_block, |
| 171 | esp->command_block_dma); |
| 172 | fail_unmap_regs: |
| 173 | fail_unlink: |
| 174 | scsi_host_put(host); |
| 175 | fail: |
| 176 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 179 | static int esp_jazz_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 181 | struct esp *esp = dev_get_drvdata(&dev->dev); |
| 182 | unsigned int irq = esp->host->irq; |
| 183 | |
| 184 | scsi_esp_unregister(esp); |
| 185 | |
| 186 | free_irq(irq, esp); |
| 187 | dma_free_coherent(esp->dev, 16, |
| 188 | esp->command_block, |
| 189 | esp->command_block_dma); |
| 190 | |
| 191 | scsi_host_put(esp->host); |
| 192 | |
| 193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Kay Sievers | ecc1241e | 2008-04-18 13:57:19 -0700 | [diff] [blame] | 196 | /* work with hotplug and coldplug */ |
| 197 | MODULE_ALIAS("platform:jazz_esp"); |
| 198 | |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 199 | static struct platform_driver esp_jazz_driver = { |
| 200 | .probe = esp_jazz_probe, |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 201 | .remove = esp_jazz_remove, |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 202 | .driver = { |
| 203 | .name = "jazz_esp", |
| 204 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | }; |
Liu Shixin | 7fc83de | 2020-09-14 14:54:03 +0800 | [diff] [blame] | 206 | module_platform_driver(esp_jazz_driver); |
Thomas Bogendoerfer | 352e921 | 2007-05-22 10:13:19 +0200 | [diff] [blame] | 207 | |
| 208 | MODULE_DESCRIPTION("JAZZ ESP SCSI driver"); |
| 209 | MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)"); |
| 210 | MODULE_LICENSE("GPL"); |
| 211 | MODULE_VERSION(DRV_VERSION); |