David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 1 | /* sun_esp.c: ESP front-end for Sparc SBUS systems. |
| 2 | * |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/types.h> |
David S. Miller | 6025dfe | 2007-04-29 16:12:29 -0700 | [diff] [blame] | 8 | #include <linux/delay.h> |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
David S. Miller | 738f2b7 | 2008-08-27 18:09:11 -0700 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 13 | #include <linux/of.h> |
| 14 | #include <linux/of_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 16 | |
| 17 | #include <asm/irq.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/dma.h> |
| 20 | |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 21 | #include <scsi/scsi_host.h> |
| 22 | |
| 23 | #include "esp_scsi.h" |
| 24 | |
| 25 | #define DRV_MODULE_NAME "sun_esp" |
| 26 | #define PFX DRV_MODULE_NAME ": " |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 27 | #define DRV_VERSION "1.100" |
| 28 | #define DRV_MODULE_RELDATE "August 27, 2008" |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 29 | |
| 30 | #define dma_read32(REG) \ |
| 31 | sbus_readl(esp->dma_regs + (REG)) |
| 32 | #define dma_write32(VAL, REG) \ |
| 33 | sbus_writel((VAL), esp->dma_regs + (REG)) |
| 34 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 35 | /* DVMA chip revisions */ |
| 36 | enum dvma_rev { |
| 37 | dvmarev0, |
| 38 | dvmaesc1, |
| 39 | dvmarev1, |
| 40 | dvmarev2, |
| 41 | dvmarev3, |
| 42 | dvmarevplus, |
| 43 | dvmahme |
| 44 | }; |
| 45 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 46 | static int esp_sbus_setup_dma(struct esp *esp, struct platform_device *dma_of) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 47 | { |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 48 | esp->dma = dma_of; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 49 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 50 | esp->dma_regs = of_ioremap(&dma_of->resource[0], 0, |
| 51 | resource_size(&dma_of->resource[0]), |
| 52 | "espdma"); |
| 53 | if (!esp->dma_regs) |
| 54 | return -ENOMEM; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 55 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 56 | switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) { |
| 57 | case DMA_VERS0: |
| 58 | esp->dmarev = dvmarev0; |
| 59 | break; |
| 60 | case DMA_ESCV1: |
| 61 | esp->dmarev = dvmaesc1; |
| 62 | break; |
| 63 | case DMA_VERS1: |
| 64 | esp->dmarev = dvmarev1; |
| 65 | break; |
| 66 | case DMA_VERS2: |
| 67 | esp->dmarev = dvmarev2; |
| 68 | break; |
| 69 | case DMA_VERHME: |
| 70 | esp->dmarev = dvmahme; |
| 71 | break; |
| 72 | case DMA_VERSPLUS: |
| 73 | esp->dmarev = dvmarevplus; |
| 74 | break; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 75 | } |
| 76 | |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | |
| 79 | } |
| 80 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 81 | static int esp_sbus_map_regs(struct esp *esp, int hme) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 82 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 83 | struct platform_device *op = esp->dev; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 84 | struct resource *res; |
| 85 | |
| 86 | /* On HME, two reg sets exist, first is DVMA, |
| 87 | * second is ESP registers. |
| 88 | */ |
| 89 | if (hme) |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 90 | res = &op->resource[1]; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 91 | else |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 92 | res = &op->resource[0]; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 93 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 94 | esp->regs = of_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP"); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 95 | if (!esp->regs) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 101 | static int esp_sbus_map_command_block(struct esp *esp) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 102 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 103 | struct platform_device *op = esp->dev; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 104 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 105 | esp->command_block = dma_alloc_coherent(&op->dev, 16, |
David S. Miller | 738f2b7 | 2008-08-27 18:09:11 -0700 | [diff] [blame] | 106 | &esp->command_block_dma, |
| 107 | GFP_ATOMIC); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 108 | if (!esp->command_block) |
| 109 | return -ENOMEM; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 113 | static int esp_sbus_register_irq(struct esp *esp) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 114 | { |
| 115 | struct Scsi_Host *host = esp->host; |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 116 | struct platform_device *op = esp->dev; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 117 | |
Grant Likely | 1636f8a | 2010-06-18 11:09:58 -0600 | [diff] [blame] | 118 | host->irq = op->archdata.irqs[0]; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 119 | return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp); |
| 120 | } |
| 121 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 122 | static void esp_get_scsi_id(struct esp *esp, struct platform_device *espdma) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 123 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 124 | struct platform_device *op = esp->dev; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 125 | struct device_node *dp; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 126 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 127 | dp = op->dev.of_node; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 128 | esp->scsi_id = of_getintprop_default(dp, "initiator-id", 0xff); |
| 129 | if (esp->scsi_id != 0xff) |
| 130 | goto done; |
| 131 | |
| 132 | esp->scsi_id = of_getintprop_default(dp, "scsi-initiator-id", 0xff); |
| 133 | if (esp->scsi_id != 0xff) |
| 134 | goto done; |
| 135 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 136 | esp->scsi_id = of_getintprop_default(espdma->dev.of_node, |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 137 | "scsi-initiator-id", 7); |
| 138 | |
| 139 | done: |
| 140 | esp->host->this_id = esp->scsi_id; |
| 141 | esp->scsi_id_mask = (1 << esp->scsi_id); |
| 142 | } |
| 143 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 144 | static void esp_get_differential(struct esp *esp) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 145 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 146 | struct platform_device *op = esp->dev; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 147 | struct device_node *dp; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 148 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 149 | dp = op->dev.of_node; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 150 | if (of_find_property(dp, "differential", NULL)) |
| 151 | esp->flags |= ESP_FLAG_DIFFERENTIAL; |
| 152 | else |
| 153 | esp->flags &= ~ESP_FLAG_DIFFERENTIAL; |
| 154 | } |
| 155 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 156 | static void esp_get_clock_params(struct esp *esp) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 157 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 158 | struct platform_device *op = esp->dev; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 159 | struct device_node *bus_dp, *dp; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 160 | int fmhz; |
| 161 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 162 | dp = op->dev.of_node; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 163 | bus_dp = dp->parent; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 164 | |
| 165 | fmhz = of_getintprop_default(dp, "clock-frequency", 0); |
| 166 | if (fmhz == 0) |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 167 | fmhz = of_getintprop_default(bus_dp, "clock-frequency", 0); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 168 | |
| 169 | esp->cfreq = fmhz; |
| 170 | } |
| 171 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 172 | static void esp_get_bursts(struct esp *esp, struct platform_device *dma_of) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 173 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 174 | struct device_node *dma_dp = dma_of->dev.of_node; |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 175 | struct platform_device *op = esp->dev; |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 176 | struct device_node *dp; |
| 177 | u8 bursts, val; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 178 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 179 | dp = op->dev.of_node; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 180 | bursts = of_getintprop_default(dp, "burst-sizes", 0xff); |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 181 | val = of_getintprop_default(dma_dp, "burst-sizes", 0xff); |
| 182 | if (val != 0xff) |
| 183 | bursts &= val; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 184 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 185 | val = of_getintprop_default(dma_dp->parent, "burst-sizes", 0xff); |
| 186 | if (val != 0xff) |
| 187 | bursts &= val; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 188 | |
| 189 | if (bursts == 0xff || |
| 190 | (bursts & DMA_BURST16) == 0 || |
| 191 | (bursts & DMA_BURST32) == 0) |
| 192 | bursts = (DMA_BURST32 - 1); |
| 193 | |
| 194 | esp->bursts = bursts; |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 197 | static void esp_sbus_get_props(struct esp *esp, struct platform_device *espdma) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 198 | { |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 199 | esp_get_scsi_id(esp, espdma); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 200 | esp_get_differential(esp); |
| 201 | esp_get_clock_params(esp); |
| 202 | esp_get_bursts(esp, espdma); |
| 203 | } |
| 204 | |
| 205 | static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg) |
| 206 | { |
| 207 | sbus_writeb(val, esp->regs + (reg * 4UL)); |
| 208 | } |
| 209 | |
| 210 | static u8 sbus_esp_read8(struct esp *esp, unsigned long reg) |
| 211 | { |
| 212 | return sbus_readb(esp->regs + (reg * 4UL)); |
| 213 | } |
| 214 | |
| 215 | static dma_addr_t sbus_esp_map_single(struct esp *esp, void *buf, |
| 216 | size_t sz, int dir) |
| 217 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 218 | struct platform_device *op = esp->dev; |
David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 219 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 220 | return dma_map_single(&op->dev, buf, sz, dir); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static int sbus_esp_map_sg(struct esp *esp, struct scatterlist *sg, |
| 224 | int num_sg, int dir) |
| 225 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 226 | struct platform_device *op = esp->dev; |
David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 227 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 228 | return dma_map_sg(&op->dev, sg, num_sg, dir); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void sbus_esp_unmap_single(struct esp *esp, dma_addr_t addr, |
| 232 | size_t sz, int dir) |
| 233 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 234 | struct platform_device *op = esp->dev; |
David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 235 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 236 | dma_unmap_single(&op->dev, addr, sz, dir); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static void sbus_esp_unmap_sg(struct esp *esp, struct scatterlist *sg, |
| 240 | int num_sg, int dir) |
| 241 | { |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 242 | struct platform_device *op = esp->dev; |
David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 243 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 244 | dma_unmap_sg(&op->dev, sg, num_sg, dir); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int sbus_esp_irq_pending(struct esp *esp) |
| 248 | { |
| 249 | if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR)) |
| 250 | return 1; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static void sbus_esp_reset_dma(struct esp *esp) |
| 255 | { |
| 256 | int can_do_burst16, can_do_burst32, can_do_burst64; |
| 257 | int can_do_sbus64, lim; |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 258 | struct platform_device *op; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 259 | u32 val; |
| 260 | |
| 261 | can_do_burst16 = (esp->bursts & DMA_BURST16) != 0; |
| 262 | can_do_burst32 = (esp->bursts & DMA_BURST32) != 0; |
| 263 | can_do_burst64 = 0; |
| 264 | can_do_sbus64 = 0; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 265 | op = esp->dev; |
David S. Miller | 63237ee | 2008-08-26 23:33:42 -0700 | [diff] [blame] | 266 | if (sbus_can_dma_64bit()) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 267 | can_do_sbus64 = 1; |
David S. Miller | 63237ee | 2008-08-26 23:33:42 -0700 | [diff] [blame] | 268 | if (sbus_can_burst64()) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 269 | can_do_burst64 = (esp->bursts & DMA_BURST64) != 0; |
| 270 | |
| 271 | /* Put the DVMA into a known state. */ |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 272 | if (esp->dmarev != dvmahme) { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 273 | val = dma_read32(DMA_CSR); |
| 274 | dma_write32(val | DMA_RST_SCSI, DMA_CSR); |
| 275 | dma_write32(val & ~DMA_RST_SCSI, DMA_CSR); |
| 276 | } |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 277 | switch (esp->dmarev) { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 278 | case dvmahme: |
| 279 | dma_write32(DMA_RESET_FAS366, DMA_CSR); |
| 280 | dma_write32(DMA_RST_SCSI, DMA_CSR); |
| 281 | |
| 282 | esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS | |
| 283 | DMA_SCSI_DISAB | DMA_INT_ENAB); |
| 284 | |
| 285 | esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE | |
| 286 | DMA_BRST_SZ); |
| 287 | |
| 288 | if (can_do_burst64) |
| 289 | esp->prev_hme_dmacsr |= DMA_BRST64; |
| 290 | else if (can_do_burst32) |
| 291 | esp->prev_hme_dmacsr |= DMA_BRST32; |
| 292 | |
| 293 | if (can_do_sbus64) { |
| 294 | esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 295 | sbus_set_sbus64(&op->dev, esp->bursts); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | lim = 1000; |
| 299 | while (dma_read32(DMA_CSR) & DMA_PEND_READ) { |
| 300 | if (--lim == 0) { |
| 301 | printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ " |
| 302 | "will not clear!\n", |
| 303 | esp->host->unique_id); |
| 304 | break; |
| 305 | } |
| 306 | udelay(1); |
| 307 | } |
| 308 | |
| 309 | dma_write32(0, DMA_CSR); |
| 310 | dma_write32(esp->prev_hme_dmacsr, DMA_CSR); |
| 311 | |
| 312 | dma_write32(0, DMA_ADDR); |
| 313 | break; |
| 314 | |
| 315 | case dvmarev2: |
| 316 | if (esp->rev != ESP100) { |
| 317 | val = dma_read32(DMA_CSR); |
| 318 | dma_write32(val | DMA_3CLKS, DMA_CSR); |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case dvmarev3: |
| 323 | val = dma_read32(DMA_CSR); |
| 324 | val &= ~DMA_3CLKS; |
| 325 | val |= DMA_2CLKS; |
| 326 | if (can_do_burst32) { |
| 327 | val &= ~DMA_BRST_SZ; |
| 328 | val |= DMA_BRST32; |
| 329 | } |
| 330 | dma_write32(val, DMA_CSR); |
| 331 | break; |
| 332 | |
| 333 | case dvmaesc1: |
| 334 | val = dma_read32(DMA_CSR); |
| 335 | val |= DMA_ADD_ENABLE; |
| 336 | val &= ~DMA_BCNT_ENAB; |
| 337 | if (!can_do_burst32 && can_do_burst16) { |
| 338 | val |= DMA_ESC_BURST; |
| 339 | } else { |
| 340 | val &= ~(DMA_ESC_BURST); |
| 341 | } |
| 342 | dma_write32(val, DMA_CSR); |
| 343 | break; |
| 344 | |
| 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | /* Enable interrupts. */ |
| 350 | val = dma_read32(DMA_CSR); |
| 351 | dma_write32(val | DMA_INT_ENAB, DMA_CSR); |
| 352 | } |
| 353 | |
| 354 | static void sbus_esp_dma_drain(struct esp *esp) |
| 355 | { |
| 356 | u32 csr; |
| 357 | int lim; |
| 358 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 359 | if (esp->dmarev == dvmahme) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 360 | return; |
| 361 | |
| 362 | csr = dma_read32(DMA_CSR); |
| 363 | if (!(csr & DMA_FIFO_ISDRAIN)) |
| 364 | return; |
| 365 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 366 | if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 367 | dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR); |
| 368 | |
| 369 | lim = 1000; |
| 370 | while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) { |
| 371 | if (--lim == 0) { |
| 372 | printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n", |
| 373 | esp->host->unique_id); |
| 374 | break; |
| 375 | } |
| 376 | udelay(1); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void sbus_esp_dma_invalidate(struct esp *esp) |
| 381 | { |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 382 | if (esp->dmarev == dvmahme) { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 383 | dma_write32(DMA_RST_SCSI, DMA_CSR); |
| 384 | |
| 385 | esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr | |
| 386 | (DMA_PARITY_OFF | DMA_2CLKS | |
| 387 | DMA_SCSI_DISAB | DMA_INT_ENAB)) & |
| 388 | ~(DMA_ST_WRITE | DMA_ENABLE)); |
| 389 | |
| 390 | dma_write32(0, DMA_CSR); |
| 391 | dma_write32(esp->prev_hme_dmacsr, DMA_CSR); |
| 392 | |
| 393 | /* This is necessary to avoid having the SCSI channel |
| 394 | * engine lock up on us. |
| 395 | */ |
| 396 | dma_write32(0, DMA_ADDR); |
| 397 | } else { |
| 398 | u32 val; |
| 399 | int lim; |
| 400 | |
| 401 | lim = 1000; |
| 402 | while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) { |
| 403 | if (--lim == 0) { |
| 404 | printk(KERN_ALERT PFX "esp%d: DMA will not " |
| 405 | "invalidate!\n", esp->host->unique_id); |
| 406 | break; |
| 407 | } |
| 408 | udelay(1); |
| 409 | } |
| 410 | |
| 411 | val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB); |
| 412 | val |= DMA_FIFO_INV; |
| 413 | dma_write32(val, DMA_CSR); |
| 414 | val &= ~DMA_FIFO_INV; |
| 415 | dma_write32(val, DMA_CSR); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count, |
| 420 | u32 dma_count, int write, u8 cmd) |
| 421 | { |
| 422 | u32 csr; |
| 423 | |
| 424 | BUG_ON(!(cmd & ESP_CMD_DMA)); |
| 425 | |
| 426 | sbus_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW); |
| 427 | sbus_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED); |
| 428 | if (esp->rev == FASHME) { |
| 429 | sbus_esp_write8(esp, (esp_count >> 16) & 0xff, FAS_RLO); |
| 430 | sbus_esp_write8(esp, 0, FAS_RHI); |
| 431 | |
| 432 | scsi_esp_cmd(esp, cmd); |
| 433 | |
| 434 | csr = esp->prev_hme_dmacsr; |
| 435 | csr |= DMA_SCSI_DISAB | DMA_ENABLE; |
| 436 | if (write) |
| 437 | csr |= DMA_ST_WRITE; |
| 438 | else |
| 439 | csr &= ~DMA_ST_WRITE; |
| 440 | esp->prev_hme_dmacsr = csr; |
| 441 | |
| 442 | dma_write32(dma_count, DMA_COUNT); |
| 443 | dma_write32(addr, DMA_ADDR); |
| 444 | dma_write32(csr, DMA_CSR); |
| 445 | } else { |
| 446 | csr = dma_read32(DMA_CSR); |
| 447 | csr |= DMA_ENABLE; |
| 448 | if (write) |
| 449 | csr |= DMA_ST_WRITE; |
| 450 | else |
| 451 | csr &= ~DMA_ST_WRITE; |
| 452 | dma_write32(csr, DMA_CSR); |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 453 | if (esp->dmarev == dvmaesc1) { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 454 | u32 end = PAGE_ALIGN(addr + dma_count + 16U); |
| 455 | dma_write32(end - addr, DMA_COUNT); |
| 456 | } |
| 457 | dma_write32(addr, DMA_ADDR); |
| 458 | |
| 459 | scsi_esp_cmd(esp, cmd); |
| 460 | } |
| 461 | |
| 462 | } |
| 463 | |
| 464 | static int sbus_esp_dma_error(struct esp *esp) |
| 465 | { |
| 466 | u32 csr = dma_read32(DMA_CSR); |
| 467 | |
| 468 | if (csr & DMA_HNDL_ERROR) |
| 469 | return 1; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static const struct esp_driver_ops sbus_esp_ops = { |
| 475 | .esp_write8 = sbus_esp_write8, |
| 476 | .esp_read8 = sbus_esp_read8, |
| 477 | .map_single = sbus_esp_map_single, |
| 478 | .map_sg = sbus_esp_map_sg, |
| 479 | .unmap_single = sbus_esp_unmap_single, |
| 480 | .unmap_sg = sbus_esp_unmap_sg, |
| 481 | .irq_pending = sbus_esp_irq_pending, |
| 482 | .reset_dma = sbus_esp_reset_dma, |
| 483 | .dma_drain = sbus_esp_dma_drain, |
| 484 | .dma_invalidate = sbus_esp_dma_invalidate, |
| 485 | .send_dma_cmd = sbus_esp_send_dma_cmd, |
| 486 | .dma_error = sbus_esp_dma_error, |
| 487 | }; |
| 488 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 489 | static int esp_sbus_probe_one(struct platform_device *op, |
| 490 | struct platform_device *espdma, int hme) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 491 | { |
| 492 | struct scsi_host_template *tpnt = &scsi_esp_template; |
| 493 | struct Scsi_Host *host; |
| 494 | struct esp *esp; |
| 495 | int err; |
| 496 | |
| 497 | host = scsi_host_alloc(tpnt, sizeof(struct esp)); |
| 498 | |
| 499 | err = -ENOMEM; |
| 500 | if (!host) |
| 501 | goto fail; |
| 502 | |
| 503 | host->max_id = (hme ? 16 : 8); |
Christoph Hellwig | 2b14ec7 | 2007-05-31 20:12:32 +0200 | [diff] [blame] | 504 | esp = shost_priv(host); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 505 | |
| 506 | esp->host = host; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 507 | esp->dev = op; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 508 | esp->ops = &sbus_esp_ops; |
| 509 | |
| 510 | if (hme) |
| 511 | esp->flags |= ESP_FLAG_WIDE_CAPABLE; |
| 512 | |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 513 | err = esp_sbus_setup_dma(esp, espdma); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 514 | if (err < 0) |
| 515 | goto fail_unlink; |
| 516 | |
| 517 | err = esp_sbus_map_regs(esp, hme); |
| 518 | if (err < 0) |
| 519 | goto fail_unlink; |
| 520 | |
| 521 | err = esp_sbus_map_command_block(esp); |
| 522 | if (err < 0) |
| 523 | goto fail_unmap_regs; |
| 524 | |
| 525 | err = esp_sbus_register_irq(esp); |
| 526 | if (err < 0) |
| 527 | goto fail_unmap_command_block; |
| 528 | |
| 529 | esp_sbus_get_props(esp, espdma); |
| 530 | |
| 531 | /* Before we try to touch the ESP chip, ESC1 dma can |
| 532 | * come up with the reset bit set, so make sure that |
| 533 | * is clear first. |
| 534 | */ |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 535 | if (esp->dmarev == dvmaesc1) { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 536 | u32 val = dma_read32(DMA_CSR); |
| 537 | |
| 538 | dma_write32(val & ~DMA_RST_SCSI, DMA_CSR); |
| 539 | } |
| 540 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 541 | dev_set_drvdata(&op->dev, esp); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 542 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 543 | err = scsi_esp_register(esp, &op->dev); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 544 | if (err) |
| 545 | goto fail_free_irq; |
| 546 | |
| 547 | return 0; |
| 548 | |
| 549 | fail_free_irq: |
| 550 | free_irq(host->irq, esp); |
| 551 | fail_unmap_command_block: |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 552 | dma_free_coherent(&op->dev, 16, |
David S. Miller | 738f2b7 | 2008-08-27 18:09:11 -0700 | [diff] [blame] | 553 | esp->command_block, |
| 554 | esp->command_block_dma); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 555 | fail_unmap_regs: |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 556 | of_iounmap(&op->resource[(hme ? 1 : 0)], esp->regs, SBUS_ESP_REG_SIZE); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 557 | fail_unlink: |
| 558 | scsi_host_put(host); |
| 559 | fail: |
| 560 | return err; |
| 561 | } |
| 562 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 563 | static int esp_sbus_probe(struct platform_device *op) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 564 | { |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 565 | struct device_node *dma_node = NULL; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 566 | struct device_node *dp = op->dev.of_node; |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 567 | struct platform_device *dma_of = NULL; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 568 | int hme = 0; |
| 569 | |
| 570 | if (dp->parent && |
| 571 | (!strcmp(dp->parent->name, "espdma") || |
| 572 | !strcmp(dp->parent->name, "dma"))) |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 573 | dma_node = dp->parent; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 574 | else if (!strcmp(dp->name, "SUNW,fas")) { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 575 | dma_node = op->dev.of_node; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 576 | hme = 1; |
| 577 | } |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 578 | if (dma_node) |
| 579 | dma_of = of_find_device_by_node(dma_node); |
| 580 | if (!dma_of) |
| 581 | return -ENODEV; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 582 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 583 | return esp_sbus_probe_one(op, dma_of, hme); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 586 | static int esp_sbus_remove(struct platform_device *op) |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 587 | { |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 588 | struct esp *esp = dev_get_drvdata(&op->dev); |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 589 | struct platform_device *dma_of = esp->dma; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 590 | unsigned int irq = esp->host->irq; |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 591 | bool is_hme; |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 592 | u32 val; |
| 593 | |
| 594 | scsi_esp_unregister(esp); |
| 595 | |
| 596 | /* Disable interrupts. */ |
| 597 | val = dma_read32(DMA_CSR); |
| 598 | dma_write32(val & ~DMA_INT_ENAB, DMA_CSR); |
| 599 | |
| 600 | free_irq(irq, esp); |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 601 | |
| 602 | is_hme = (esp->dmarev == dvmahme); |
| 603 | |
| 604 | dma_free_coherent(&op->dev, 16, |
David S. Miller | 738f2b7 | 2008-08-27 18:09:11 -0700 | [diff] [blame] | 605 | esp->command_block, |
| 606 | esp->command_block_dma); |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 607 | of_iounmap(&op->resource[(is_hme ? 1 : 0)], esp->regs, |
| 608 | SBUS_ESP_REG_SIZE); |
David S. Miller | 334ae61 | 2008-08-27 17:01:57 -0700 | [diff] [blame] | 609 | of_iounmap(&dma_of->resource[0], esp->dma_regs, |
| 610 | resource_size(&dma_of->resource[0])); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 611 | |
| 612 | scsi_host_put(esp->host); |
| 613 | |
David S. Miller | 05bb5e9 | 2008-08-27 00:20:58 -0700 | [diff] [blame] | 614 | dev_set_drvdata(&op->dev, NULL); |
| 615 | |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 619 | static const struct of_device_id esp_match[] = { |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 620 | { |
| 621 | .name = "SUNW,esp", |
| 622 | }, |
| 623 | { |
| 624 | .name = "SUNW,fas", |
| 625 | }, |
| 626 | { |
| 627 | .name = "esp", |
| 628 | }, |
| 629 | {}, |
| 630 | }; |
| 631 | MODULE_DEVICE_TABLE(of, esp_match); |
| 632 | |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 633 | static struct platform_driver esp_sbus_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 634 | .driver = { |
| 635 | .name = "esp", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 636 | .of_match_table = esp_match, |
| 637 | }, |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 638 | .probe = esp_sbus_probe, |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 639 | .remove = esp_sbus_remove, |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | static int __init sunesp_init(void) |
| 643 | { |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 644 | return platform_driver_register(&esp_sbus_driver); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static void __exit sunesp_exit(void) |
| 648 | { |
Grant Likely | 4ebb24f | 2011-02-22 20:01:33 -0700 | [diff] [blame] | 649 | platform_driver_unregister(&esp_sbus_driver); |
David S. Miller | cd9ad58 | 2007-04-26 21:19:23 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | MODULE_DESCRIPTION("Sun ESP SCSI driver"); |
| 653 | MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); |
| 654 | MODULE_LICENSE("GPL"); |
| 655 | MODULE_VERSION(DRV_VERSION); |
| 656 | |
| 657 | module_init(sunesp_init); |
| 658 | module_exit(sunesp_exit); |