blob: 964130d2c8a65002b4de134dc42e80c38218ea52 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David S. Millercd9ad582007-04-26 21:19:23 -07002/* sun_esp.c: ESP front-end for Sparc SBUS systems.
3 *
David S. Miller334ae612008-08-27 17:01:57 -07004 * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
David S. Millercd9ad582007-04-26 21:19:23 -07005 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
David S. Miller6025dfe2007-04-29 16:12:29 -07009#include <linux/delay.h>
David S. Millercd9ad582007-04-26 21:19:23 -070010#include <linux/module.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070011#include <linux/mm.h>
David S. Millercd9ad582007-04-26 21:19:23 -070012#include <linux/init.h>
David S. Miller738f2b72008-08-27 18:09:11 -070013#include <linux/dma-mapping.h>
David S. Miller05bb5e92008-08-27 00:20:58 -070014#include <linux/of.h>
15#include <linux/of_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
David S. Millercd9ad582007-04-26 21:19:23 -070017
18#include <asm/irq.h>
19#include <asm/io.h>
20#include <asm/dma.h>
21
David S. Millercd9ad582007-04-26 21:19:23 -070022#include <scsi/scsi_host.h>
23
24#include "esp_scsi.h"
25
26#define DRV_MODULE_NAME "sun_esp"
27#define PFX DRV_MODULE_NAME ": "
David S. Miller05bb5e92008-08-27 00:20:58 -070028#define DRV_VERSION "1.100"
29#define DRV_MODULE_RELDATE "August 27, 2008"
David S. Millercd9ad582007-04-26 21:19:23 -070030
31#define dma_read32(REG) \
32 sbus_readl(esp->dma_regs + (REG))
33#define dma_write32(VAL, REG) \
34 sbus_writel((VAL), esp->dma_regs + (REG))
35
David S. Miller334ae612008-08-27 17:01:57 -070036/* DVMA chip revisions */
37enum dvma_rev {
38 dvmarev0,
39 dvmaesc1,
40 dvmarev1,
41 dvmarev2,
42 dvmarev3,
43 dvmarevplus,
44 dvmahme
45};
46
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080047static int esp_sbus_setup_dma(struct esp *esp, struct platform_device *dma_of)
David S. Millercd9ad582007-04-26 21:19:23 -070048{
David S. Miller334ae612008-08-27 17:01:57 -070049 esp->dma = dma_of;
David S. Millercd9ad582007-04-26 21:19:23 -070050
David S. Miller334ae612008-08-27 17:01:57 -070051 esp->dma_regs = of_ioremap(&dma_of->resource[0], 0,
52 resource_size(&dma_of->resource[0]),
53 "espdma");
54 if (!esp->dma_regs)
55 return -ENOMEM;
David S. Millercd9ad582007-04-26 21:19:23 -070056
David S. Miller334ae612008-08-27 17:01:57 -070057 switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) {
58 case DMA_VERS0:
59 esp->dmarev = dvmarev0;
60 break;
61 case DMA_ESCV1:
62 esp->dmarev = dvmaesc1;
63 break;
64 case DMA_VERS1:
65 esp->dmarev = dvmarev1;
66 break;
67 case DMA_VERS2:
68 esp->dmarev = dvmarev2;
69 break;
70 case DMA_VERHME:
71 esp->dmarev = dvmahme;
72 break;
73 case DMA_VERSPLUS:
74 esp->dmarev = dvmarevplus;
75 break;
David S. Millercd9ad582007-04-26 21:19:23 -070076 }
77
David S. Millercd9ad582007-04-26 21:19:23 -070078 return 0;
79
80}
81
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080082static int esp_sbus_map_regs(struct esp *esp, int hme)
David S. Millercd9ad582007-04-26 21:19:23 -070083{
Christoph Hellwig98cda6a2018-10-13 09:26:25 +020084 struct platform_device *op = to_platform_device(esp->dev);
David S. Millercd9ad582007-04-26 21:19:23 -070085 struct resource *res;
86
87 /* On HME, two reg sets exist, first is DVMA,
88 * second is ESP registers.
89 */
90 if (hme)
David S. Miller05bb5e92008-08-27 00:20:58 -070091 res = &op->resource[1];
David S. Millercd9ad582007-04-26 21:19:23 -070092 else
David S. Miller05bb5e92008-08-27 00:20:58 -070093 res = &op->resource[0];
David S. Millercd9ad582007-04-26 21:19:23 -070094
David S. Miller05bb5e92008-08-27 00:20:58 -070095 esp->regs = of_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP");
David S. Millercd9ad582007-04-26 21:19:23 -070096 if (!esp->regs)
97 return -ENOMEM;
98
99 return 0;
100}
101
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800102static int esp_sbus_map_command_block(struct esp *esp)
David S. Millercd9ad582007-04-26 21:19:23 -0700103{
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200104 esp->command_block = dma_alloc_coherent(esp->dev, 16,
David S. Miller738f2b72008-08-27 18:09:11 -0700105 &esp->command_block_dma,
Christoph Hellwig10c0cd32018-10-13 09:26:24 +0200106 GFP_KERNEL);
David S. Millercd9ad582007-04-26 21:19:23 -0700107 if (!esp->command_block)
108 return -ENOMEM;
109 return 0;
110}
111
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800112static int esp_sbus_register_irq(struct esp *esp)
David S. Millercd9ad582007-04-26 21:19:23 -0700113{
114 struct Scsi_Host *host = esp->host;
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200115 struct platform_device *op = to_platform_device(esp->dev);
David S. Millercd9ad582007-04-26 21:19:23 -0700116
Grant Likely1636f8a2010-06-18 11:09:58 -0600117 host->irq = op->archdata.irqs[0];
David S. Millercd9ad582007-04-26 21:19:23 -0700118 return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
119}
120
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800121static void esp_get_scsi_id(struct esp *esp, struct platform_device *espdma)
David S. Millercd9ad582007-04-26 21:19:23 -0700122{
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200123 struct platform_device *op = to_platform_device(esp->dev);
David S. Miller05bb5e92008-08-27 00:20:58 -0700124 struct device_node *dp;
David S. Millercd9ad582007-04-26 21:19:23 -0700125
Grant Likely61c7a082010-04-13 16:12:29 -0700126 dp = op->dev.of_node;
David S. Millercd9ad582007-04-26 21:19:23 -0700127 esp->scsi_id = of_getintprop_default(dp, "initiator-id", 0xff);
128 if (esp->scsi_id != 0xff)
129 goto done;
130
131 esp->scsi_id = of_getintprop_default(dp, "scsi-initiator-id", 0xff);
132 if (esp->scsi_id != 0xff)
133 goto done;
134
Grant Likely61c7a082010-04-13 16:12:29 -0700135 esp->scsi_id = of_getintprop_default(espdma->dev.of_node,
David S. Millercd9ad582007-04-26 21:19:23 -0700136 "scsi-initiator-id", 7);
137
138done:
139 esp->host->this_id = esp->scsi_id;
140 esp->scsi_id_mask = (1 << esp->scsi_id);
141}
142
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800143static void esp_get_differential(struct esp *esp)
David S. Millercd9ad582007-04-26 21:19:23 -0700144{
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200145 struct platform_device *op = to_platform_device(esp->dev);
David S. Miller05bb5e92008-08-27 00:20:58 -0700146 struct device_node *dp;
David S. Millercd9ad582007-04-26 21:19:23 -0700147
Grant Likely61c7a082010-04-13 16:12:29 -0700148 dp = op->dev.of_node;
David S. Millercd9ad582007-04-26 21:19:23 -0700149 if (of_find_property(dp, "differential", NULL))
150 esp->flags |= ESP_FLAG_DIFFERENTIAL;
151 else
152 esp->flags &= ~ESP_FLAG_DIFFERENTIAL;
153}
154
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800155static void esp_get_clock_params(struct esp *esp)
David S. Millercd9ad582007-04-26 21:19:23 -0700156{
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200157 struct platform_device *op = to_platform_device(esp->dev);
David S. Miller05bb5e92008-08-27 00:20:58 -0700158 struct device_node *bus_dp, *dp;
David S. Millercd9ad582007-04-26 21:19:23 -0700159 int fmhz;
160
Grant Likely61c7a082010-04-13 16:12:29 -0700161 dp = op->dev.of_node;
David S. Miller05bb5e92008-08-27 00:20:58 -0700162 bus_dp = dp->parent;
David S. Millercd9ad582007-04-26 21:19:23 -0700163
164 fmhz = of_getintprop_default(dp, "clock-frequency", 0);
165 if (fmhz == 0)
David S. Miller05bb5e92008-08-27 00:20:58 -0700166 fmhz = of_getintprop_default(bus_dp, "clock-frequency", 0);
David S. Millercd9ad582007-04-26 21:19:23 -0700167
168 esp->cfreq = fmhz;
169}
170
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800171static void esp_get_bursts(struct esp *esp, struct platform_device *dma_of)
David S. Millercd9ad582007-04-26 21:19:23 -0700172{
Grant Likely61c7a082010-04-13 16:12:29 -0700173 struct device_node *dma_dp = dma_of->dev.of_node;
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200174 struct platform_device *op = to_platform_device(esp->dev);
David S. Miller334ae612008-08-27 17:01:57 -0700175 struct device_node *dp;
176 u8 bursts, val;
David S. Millercd9ad582007-04-26 21:19:23 -0700177
Grant Likely61c7a082010-04-13 16:12:29 -0700178 dp = op->dev.of_node;
David S. Millercd9ad582007-04-26 21:19:23 -0700179 bursts = of_getintprop_default(dp, "burst-sizes", 0xff);
David S. Miller334ae612008-08-27 17:01:57 -0700180 val = of_getintprop_default(dma_dp, "burst-sizes", 0xff);
181 if (val != 0xff)
182 bursts &= val;
David S. Millercd9ad582007-04-26 21:19:23 -0700183
David S. Miller05bb5e92008-08-27 00:20:58 -0700184 val = of_getintprop_default(dma_dp->parent, "burst-sizes", 0xff);
185 if (val != 0xff)
186 bursts &= val;
David S. Millercd9ad582007-04-26 21:19:23 -0700187
188 if (bursts == 0xff ||
189 (bursts & DMA_BURST16) == 0 ||
190 (bursts & DMA_BURST32) == 0)
191 bursts = (DMA_BURST32 - 1);
192
193 esp->bursts = bursts;
194}
195
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800196static void esp_sbus_get_props(struct esp *esp, struct platform_device *espdma)
David S. Millercd9ad582007-04-26 21:19:23 -0700197{
David S. Miller05bb5e92008-08-27 00:20:58 -0700198 esp_get_scsi_id(esp, espdma);
David S. Millercd9ad582007-04-26 21:19:23 -0700199 esp_get_differential(esp);
200 esp_get_clock_params(esp);
201 esp_get_bursts(esp, espdma);
202}
203
204static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg)
205{
206 sbus_writeb(val, esp->regs + (reg * 4UL));
207}
208
209static u8 sbus_esp_read8(struct esp *esp, unsigned long reg)
210{
211 return sbus_readb(esp->regs + (reg * 4UL));
212}
213
David S. Millercd9ad582007-04-26 21:19:23 -0700214static int sbus_esp_irq_pending(struct esp *esp)
215{
216 if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
217 return 1;
218 return 0;
219}
220
221static void sbus_esp_reset_dma(struct esp *esp)
222{
223 int can_do_burst16, can_do_burst32, can_do_burst64;
224 int can_do_sbus64, lim;
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200225 struct platform_device *op = to_platform_device(esp->dev);
David S. Millercd9ad582007-04-26 21:19:23 -0700226 u32 val;
227
228 can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
229 can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
230 can_do_burst64 = 0;
231 can_do_sbus64 = 0;
David S. Miller63237ee2008-08-26 23:33:42 -0700232 if (sbus_can_dma_64bit())
David S. Millercd9ad582007-04-26 21:19:23 -0700233 can_do_sbus64 = 1;
David S. Miller63237ee2008-08-26 23:33:42 -0700234 if (sbus_can_burst64())
David S. Millercd9ad582007-04-26 21:19:23 -0700235 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
236
237 /* Put the DVMA into a known state. */
David S. Miller334ae612008-08-27 17:01:57 -0700238 if (esp->dmarev != dvmahme) {
David S. Millercd9ad582007-04-26 21:19:23 -0700239 val = dma_read32(DMA_CSR);
240 dma_write32(val | DMA_RST_SCSI, DMA_CSR);
241 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
242 }
David S. Miller334ae612008-08-27 17:01:57 -0700243 switch (esp->dmarev) {
David S. Millercd9ad582007-04-26 21:19:23 -0700244 case dvmahme:
245 dma_write32(DMA_RESET_FAS366, DMA_CSR);
246 dma_write32(DMA_RST_SCSI, DMA_CSR);
247
248 esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS |
249 DMA_SCSI_DISAB | DMA_INT_ENAB);
250
251 esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE |
252 DMA_BRST_SZ);
253
254 if (can_do_burst64)
255 esp->prev_hme_dmacsr |= DMA_BRST64;
256 else if (can_do_burst32)
257 esp->prev_hme_dmacsr |= DMA_BRST32;
258
259 if (can_do_sbus64) {
260 esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
David S. Miller05bb5e92008-08-27 00:20:58 -0700261 sbus_set_sbus64(&op->dev, esp->bursts);
David S. Millercd9ad582007-04-26 21:19:23 -0700262 }
263
264 lim = 1000;
265 while (dma_read32(DMA_CSR) & DMA_PEND_READ) {
266 if (--lim == 0) {
267 printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ "
268 "will not clear!\n",
269 esp->host->unique_id);
270 break;
271 }
272 udelay(1);
273 }
274
275 dma_write32(0, DMA_CSR);
276 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
277
278 dma_write32(0, DMA_ADDR);
279 break;
280
281 case dvmarev2:
282 if (esp->rev != ESP100) {
283 val = dma_read32(DMA_CSR);
284 dma_write32(val | DMA_3CLKS, DMA_CSR);
285 }
286 break;
287
288 case dvmarev3:
289 val = dma_read32(DMA_CSR);
290 val &= ~DMA_3CLKS;
291 val |= DMA_2CLKS;
292 if (can_do_burst32) {
293 val &= ~DMA_BRST_SZ;
294 val |= DMA_BRST32;
295 }
296 dma_write32(val, DMA_CSR);
297 break;
298
299 case dvmaesc1:
300 val = dma_read32(DMA_CSR);
301 val |= DMA_ADD_ENABLE;
302 val &= ~DMA_BCNT_ENAB;
303 if (!can_do_burst32 && can_do_burst16) {
304 val |= DMA_ESC_BURST;
305 } else {
306 val &= ~(DMA_ESC_BURST);
307 }
308 dma_write32(val, DMA_CSR);
309 break;
310
311 default:
312 break;
313 }
314
315 /* Enable interrupts. */
316 val = dma_read32(DMA_CSR);
317 dma_write32(val | DMA_INT_ENAB, DMA_CSR);
318}
319
320static void sbus_esp_dma_drain(struct esp *esp)
321{
322 u32 csr;
323 int lim;
324
David S. Miller334ae612008-08-27 17:01:57 -0700325 if (esp->dmarev == dvmahme)
David S. Millercd9ad582007-04-26 21:19:23 -0700326 return;
327
328 csr = dma_read32(DMA_CSR);
329 if (!(csr & DMA_FIFO_ISDRAIN))
330 return;
331
David S. Miller334ae612008-08-27 17:01:57 -0700332 if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1)
David S. Millercd9ad582007-04-26 21:19:23 -0700333 dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
334
335 lim = 1000;
336 while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
337 if (--lim == 0) {
338 printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
339 esp->host->unique_id);
340 break;
341 }
342 udelay(1);
343 }
344}
345
346static void sbus_esp_dma_invalidate(struct esp *esp)
347{
David S. Miller334ae612008-08-27 17:01:57 -0700348 if (esp->dmarev == dvmahme) {
David S. Millercd9ad582007-04-26 21:19:23 -0700349 dma_write32(DMA_RST_SCSI, DMA_CSR);
350
351 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
352 (DMA_PARITY_OFF | DMA_2CLKS |
353 DMA_SCSI_DISAB | DMA_INT_ENAB)) &
354 ~(DMA_ST_WRITE | DMA_ENABLE));
355
356 dma_write32(0, DMA_CSR);
357 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
358
359 /* This is necessary to avoid having the SCSI channel
360 * engine lock up on us.
361 */
362 dma_write32(0, DMA_ADDR);
363 } else {
364 u32 val;
365 int lim;
366
367 lim = 1000;
368 while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
369 if (--lim == 0) {
370 printk(KERN_ALERT PFX "esp%d: DMA will not "
371 "invalidate!\n", esp->host->unique_id);
372 break;
373 }
374 udelay(1);
375 }
376
377 val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
378 val |= DMA_FIFO_INV;
379 dma_write32(val, DMA_CSR);
380 val &= ~DMA_FIFO_INV;
381 dma_write32(val, DMA_CSR);
382 }
383}
384
385static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
386 u32 dma_count, int write, u8 cmd)
387{
388 u32 csr;
389
390 BUG_ON(!(cmd & ESP_CMD_DMA));
391
392 sbus_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
393 sbus_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
394 if (esp->rev == FASHME) {
395 sbus_esp_write8(esp, (esp_count >> 16) & 0xff, FAS_RLO);
396 sbus_esp_write8(esp, 0, FAS_RHI);
397
398 scsi_esp_cmd(esp, cmd);
399
400 csr = esp->prev_hme_dmacsr;
401 csr |= DMA_SCSI_DISAB | DMA_ENABLE;
402 if (write)
403 csr |= DMA_ST_WRITE;
404 else
405 csr &= ~DMA_ST_WRITE;
406 esp->prev_hme_dmacsr = csr;
407
408 dma_write32(dma_count, DMA_COUNT);
409 dma_write32(addr, DMA_ADDR);
410 dma_write32(csr, DMA_CSR);
411 } else {
412 csr = dma_read32(DMA_CSR);
413 csr |= DMA_ENABLE;
414 if (write)
415 csr |= DMA_ST_WRITE;
416 else
417 csr &= ~DMA_ST_WRITE;
418 dma_write32(csr, DMA_CSR);
David S. Miller334ae612008-08-27 17:01:57 -0700419 if (esp->dmarev == dvmaesc1) {
David S. Millercd9ad582007-04-26 21:19:23 -0700420 u32 end = PAGE_ALIGN(addr + dma_count + 16U);
421 dma_write32(end - addr, DMA_COUNT);
422 }
423 dma_write32(addr, DMA_ADDR);
424
425 scsi_esp_cmd(esp, cmd);
426 }
427
428}
429
430static int sbus_esp_dma_error(struct esp *esp)
431{
432 u32 csr = dma_read32(DMA_CSR);
433
434 if (csr & DMA_HNDL_ERROR)
435 return 1;
436
437 return 0;
438}
439
440static const struct esp_driver_ops sbus_esp_ops = {
441 .esp_write8 = sbus_esp_write8,
442 .esp_read8 = sbus_esp_read8,
David S. Millercd9ad582007-04-26 21:19:23 -0700443 .irq_pending = sbus_esp_irq_pending,
444 .reset_dma = sbus_esp_reset_dma,
445 .dma_drain = sbus_esp_dma_drain,
446 .dma_invalidate = sbus_esp_dma_invalidate,
447 .send_dma_cmd = sbus_esp_send_dma_cmd,
448 .dma_error = sbus_esp_dma_error,
449};
450
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800451static int esp_sbus_probe_one(struct platform_device *op,
452 struct platform_device *espdma, int hme)
David S. Millercd9ad582007-04-26 21:19:23 -0700453{
454 struct scsi_host_template *tpnt = &scsi_esp_template;
455 struct Scsi_Host *host;
456 struct esp *esp;
457 int err;
458
459 host = scsi_host_alloc(tpnt, sizeof(struct esp));
460
461 err = -ENOMEM;
462 if (!host)
463 goto fail;
464
465 host->max_id = (hme ? 16 : 8);
Christoph Hellwig2b14ec72007-05-31 20:12:32 +0200466 esp = shost_priv(host);
David S. Millercd9ad582007-04-26 21:19:23 -0700467
468 esp->host = host;
Christoph Hellwig98cda6a2018-10-13 09:26:25 +0200469 esp->dev = &op->dev;
David S. Millercd9ad582007-04-26 21:19:23 -0700470 esp->ops = &sbus_esp_ops;
471
472 if (hme)
473 esp->flags |= ESP_FLAG_WIDE_CAPABLE;
474
David S. Miller334ae612008-08-27 17:01:57 -0700475 err = esp_sbus_setup_dma(esp, espdma);
David S. Millercd9ad582007-04-26 21:19:23 -0700476 if (err < 0)
477 goto fail_unlink;
478
479 err = esp_sbus_map_regs(esp, hme);
480 if (err < 0)
481 goto fail_unlink;
482
483 err = esp_sbus_map_command_block(esp);
484 if (err < 0)
485 goto fail_unmap_regs;
486
487 err = esp_sbus_register_irq(esp);
488 if (err < 0)
489 goto fail_unmap_command_block;
490
491 esp_sbus_get_props(esp, espdma);
492
493 /* Before we try to touch the ESP chip, ESC1 dma can
494 * come up with the reset bit set, so make sure that
495 * is clear first.
496 */
David S. Miller334ae612008-08-27 17:01:57 -0700497 if (esp->dmarev == dvmaesc1) {
David S. Millercd9ad582007-04-26 21:19:23 -0700498 u32 val = dma_read32(DMA_CSR);
499
500 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
501 }
502
David S. Miller05bb5e92008-08-27 00:20:58 -0700503 dev_set_drvdata(&op->dev, esp);
David S. Millercd9ad582007-04-26 21:19:23 -0700504
Christoph Hellwig44b1b4d2018-10-13 09:26:26 +0200505 err = scsi_esp_register(esp);
David S. Millercd9ad582007-04-26 21:19:23 -0700506 if (err)
507 goto fail_free_irq;
508
509 return 0;
510
511fail_free_irq:
512 free_irq(host->irq, esp);
513fail_unmap_command_block:
David S. Miller05bb5e92008-08-27 00:20:58 -0700514 dma_free_coherent(&op->dev, 16,
David S. Miller738f2b72008-08-27 18:09:11 -0700515 esp->command_block,
516 esp->command_block_dma);
David S. Millercd9ad582007-04-26 21:19:23 -0700517fail_unmap_regs:
David S. Miller05bb5e92008-08-27 00:20:58 -0700518 of_iounmap(&op->resource[(hme ? 1 : 0)], esp->regs, SBUS_ESP_REG_SIZE);
David S. Millercd9ad582007-04-26 21:19:23 -0700519fail_unlink:
520 scsi_host_put(host);
521fail:
522 return err;
523}
524
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800525static int esp_sbus_probe(struct platform_device *op)
David S. Millercd9ad582007-04-26 21:19:23 -0700526{
David S. Miller334ae612008-08-27 17:01:57 -0700527 struct device_node *dma_node = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700528 struct device_node *dp = op->dev.of_node;
Grant Likely2dc11582010-08-06 09:25:50 -0600529 struct platform_device *dma_of = NULL;
David S. Millercd9ad582007-04-26 21:19:23 -0700530 int hme = 0;
Johan Hovoldf62f9ff2017-06-21 11:35:09 +0200531 int ret;
David S. Millercd9ad582007-04-26 21:19:23 -0700532
Rob Herring4b668102018-12-05 13:50:39 -0600533 if (of_node_name_eq(dp->parent, "espdma") ||
534 of_node_name_eq(dp->parent, "dma"))
David S. Miller334ae612008-08-27 17:01:57 -0700535 dma_node = dp->parent;
Rob Herring4b668102018-12-05 13:50:39 -0600536 else if (of_node_name_eq(dp, "SUNW,fas")) {
Grant Likely61c7a082010-04-13 16:12:29 -0700537 dma_node = op->dev.of_node;
David S. Millercd9ad582007-04-26 21:19:23 -0700538 hme = 1;
539 }
David S. Miller334ae612008-08-27 17:01:57 -0700540 if (dma_node)
541 dma_of = of_find_device_by_node(dma_node);
542 if (!dma_of)
543 return -ENODEV;
David S. Millercd9ad582007-04-26 21:19:23 -0700544
Johan Hovoldf62f9ff2017-06-21 11:35:09 +0200545 ret = esp_sbus_probe_one(op, dma_of, hme);
546 if (ret)
547 put_device(&dma_of->dev);
548
549 return ret;
David S. Millercd9ad582007-04-26 21:19:23 -0700550}
551
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800552static int esp_sbus_remove(struct platform_device *op)
David S. Millercd9ad582007-04-26 21:19:23 -0700553{
David S. Miller05bb5e92008-08-27 00:20:58 -0700554 struct esp *esp = dev_get_drvdata(&op->dev);
Grant Likely2dc11582010-08-06 09:25:50 -0600555 struct platform_device *dma_of = esp->dma;
David S. Millercd9ad582007-04-26 21:19:23 -0700556 unsigned int irq = esp->host->irq;
David S. Miller05bb5e92008-08-27 00:20:58 -0700557 bool is_hme;
David S. Millercd9ad582007-04-26 21:19:23 -0700558 u32 val;
559
560 scsi_esp_unregister(esp);
561
562 /* Disable interrupts. */
563 val = dma_read32(DMA_CSR);
564 dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
565
566 free_irq(irq, esp);
David S. Miller05bb5e92008-08-27 00:20:58 -0700567
568 is_hme = (esp->dmarev == dvmahme);
569
570 dma_free_coherent(&op->dev, 16,
David S. Miller738f2b72008-08-27 18:09:11 -0700571 esp->command_block,
572 esp->command_block_dma);
David S. Miller05bb5e92008-08-27 00:20:58 -0700573 of_iounmap(&op->resource[(is_hme ? 1 : 0)], esp->regs,
574 SBUS_ESP_REG_SIZE);
David S. Miller334ae612008-08-27 17:01:57 -0700575 of_iounmap(&dma_of->resource[0], esp->dma_regs,
576 resource_size(&dma_of->resource[0]));
David S. Millercd9ad582007-04-26 21:19:23 -0700577
578 scsi_host_put(esp->host);
579
David S. Miller05bb5e92008-08-27 00:20:58 -0700580 dev_set_drvdata(&op->dev, NULL);
581
Johan Hovoldf62f9ff2017-06-21 11:35:09 +0200582 put_device(&dma_of->dev);
583
David S. Millercd9ad582007-04-26 21:19:23 -0700584 return 0;
585}
586
David S. Millerfd098312008-08-31 01:23:17 -0700587static const struct of_device_id esp_match[] = {
David S. Millercd9ad582007-04-26 21:19:23 -0700588 {
589 .name = "SUNW,esp",
590 },
591 {
592 .name = "SUNW,fas",
593 },
594 {
595 .name = "esp",
596 },
597 {},
598};
599MODULE_DEVICE_TABLE(of, esp_match);
600
Grant Likely4ebb24f2011-02-22 20:01:33 -0700601static struct platform_driver esp_sbus_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700602 .driver = {
603 .name = "esp",
Grant Likely40182942010-04-13 16:13:02 -0700604 .of_match_table = esp_match,
605 },
David S. Millercd9ad582007-04-26 21:19:23 -0700606 .probe = esp_sbus_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800607 .remove = esp_sbus_remove,
David S. Millercd9ad582007-04-26 21:19:23 -0700608};
609
610static int __init sunesp_init(void)
611{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700612 return platform_driver_register(&esp_sbus_driver);
David S. Millercd9ad582007-04-26 21:19:23 -0700613}
614
615static void __exit sunesp_exit(void)
616{
Grant Likely4ebb24f2011-02-22 20:01:33 -0700617 platform_driver_unregister(&esp_sbus_driver);
David S. Millercd9ad582007-04-26 21:19:23 -0700618}
619
620MODULE_DESCRIPTION("Sun ESP SCSI driver");
621MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
622MODULE_LICENSE("GPL");
623MODULE_VERSION(DRV_VERSION);
624
625module_init(sunesp_init);
626module_exit(sunesp_exit);