1 // SPDX-License-Identifier: GPL-2.0-only
2 /* sun_esp.c: ESP front-end for Sparc SBUS systems.
4 * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/delay.h>
10 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/dma-mapping.h>
15 #include <linux/of_device.h>
16 #include <linux/gfp.h>
22 #include <scsi/scsi_host.h>
26 #define DRV_MODULE_NAME "sun_esp"
27 #define PFX DRV_MODULE_NAME ": "
28 #define DRV_VERSION "1.100"
29 #define DRV_MODULE_RELDATE "August 27, 2008"
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))
36 /* DVMA chip revisions */
47 static int esp_sbus_setup_dma(struct esp *esp, struct platform_device *dma_of)
51 esp->dma_regs = of_ioremap(&dma_of->resource[0], 0,
52 resource_size(&dma_of->resource[0]),
57 switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) {
59 esp->dmarev = dvmarev0;
62 esp->dmarev = dvmaesc1;
65 esp->dmarev = dvmarev1;
68 esp->dmarev = dvmarev2;
71 esp->dmarev = dvmahme;
74 esp->dmarev = dvmarevplus;
82 static int esp_sbus_map_regs(struct esp *esp, int hme)
84 struct platform_device *op = to_platform_device(esp->dev);
87 /* On HME, two reg sets exist, first is DVMA,
88 * second is ESP registers.
91 res = &op->resource[1];
93 res = &op->resource[0];
95 esp->regs = of_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP");
102 static int esp_sbus_map_command_block(struct esp *esp)
104 esp->command_block = dma_alloc_coherent(esp->dev, 16,
105 &esp->command_block_dma,
107 if (!esp->command_block)
112 static int esp_sbus_register_irq(struct esp *esp)
114 struct Scsi_Host *host = esp->host;
115 struct platform_device *op = to_platform_device(esp->dev);
117 host->irq = op->archdata.irqs[0];
118 return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
121 static void esp_get_scsi_id(struct esp *esp, struct platform_device *espdma)
123 struct platform_device *op = to_platform_device(esp->dev);
124 struct device_node *dp;
126 dp = op->dev.of_node;
127 esp->scsi_id = of_getintprop_default(dp, "initiator-id", 0xff);
128 if (esp->scsi_id != 0xff)
131 esp->scsi_id = of_getintprop_default(dp, "scsi-initiator-id", 0xff);
132 if (esp->scsi_id != 0xff)
135 esp->scsi_id = of_getintprop_default(espdma->dev.of_node,
136 "scsi-initiator-id", 7);
139 esp->host->this_id = esp->scsi_id;
140 esp->scsi_id_mask = (1 << esp->scsi_id);
143 static void esp_get_differential(struct esp *esp)
145 struct platform_device *op = to_platform_device(esp->dev);
146 struct device_node *dp;
148 dp = op->dev.of_node;
149 if (of_find_property(dp, "differential", NULL))
150 esp->flags |= ESP_FLAG_DIFFERENTIAL;
152 esp->flags &= ~ESP_FLAG_DIFFERENTIAL;
155 static void esp_get_clock_params(struct esp *esp)
157 struct platform_device *op = to_platform_device(esp->dev);
158 struct device_node *bus_dp, *dp;
161 dp = op->dev.of_node;
164 fmhz = of_getintprop_default(dp, "clock-frequency", 0);
166 fmhz = of_getintprop_default(bus_dp, "clock-frequency", 0);
171 static void esp_get_bursts(struct esp *esp, struct platform_device *dma_of)
173 struct device_node *dma_dp = dma_of->dev.of_node;
174 struct platform_device *op = to_platform_device(esp->dev);
175 struct device_node *dp;
178 dp = op->dev.of_node;
179 bursts = of_getintprop_default(dp, "burst-sizes", 0xff);
180 val = of_getintprop_default(dma_dp, "burst-sizes", 0xff);
184 val = of_getintprop_default(dma_dp->parent, "burst-sizes", 0xff);
188 if (bursts == 0xff ||
189 (bursts & DMA_BURST16) == 0 ||
190 (bursts & DMA_BURST32) == 0)
191 bursts = (DMA_BURST32 - 1);
193 esp->bursts = bursts;
196 static void esp_sbus_get_props(struct esp *esp, struct platform_device *espdma)
198 esp_get_scsi_id(esp, espdma);
199 esp_get_differential(esp);
200 esp_get_clock_params(esp);
201 esp_get_bursts(esp, espdma);
204 static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg)
206 sbus_writeb(val, esp->regs + (reg * 4UL));
209 static u8 sbus_esp_read8(struct esp *esp, unsigned long reg)
211 return sbus_readb(esp->regs + (reg * 4UL));
214 static int sbus_esp_irq_pending(struct esp *esp)
216 if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
221 static void sbus_esp_reset_dma(struct esp *esp)
223 int can_do_burst16, can_do_burst32, can_do_burst64;
224 int can_do_sbus64, lim;
225 struct platform_device *op = to_platform_device(esp->dev);
228 can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
229 can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
232 if (sbus_can_dma_64bit())
234 if (sbus_can_burst64())
235 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
237 /* Put the DVMA into a known state. */
238 if (esp->dmarev != dvmahme) {
239 val = dma_read32(DMA_CSR);
240 dma_write32(val | DMA_RST_SCSI, DMA_CSR);
241 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
243 switch (esp->dmarev) {
245 dma_write32(DMA_RESET_FAS366, DMA_CSR);
246 dma_write32(DMA_RST_SCSI, DMA_CSR);
248 esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS |
249 DMA_SCSI_DISAB | DMA_INT_ENAB);
251 esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE |
255 esp->prev_hme_dmacsr |= DMA_BRST64;
256 else if (can_do_burst32)
257 esp->prev_hme_dmacsr |= DMA_BRST32;
260 esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
261 sbus_set_sbus64(&op->dev, esp->bursts);
265 while (dma_read32(DMA_CSR) & DMA_PEND_READ) {
267 printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ "
269 esp->host->unique_id);
275 dma_write32(0, DMA_CSR);
276 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
278 dma_write32(0, DMA_ADDR);
282 if (esp->rev != ESP100) {
283 val = dma_read32(DMA_CSR);
284 dma_write32(val | DMA_3CLKS, DMA_CSR);
289 val = dma_read32(DMA_CSR);
292 if (can_do_burst32) {
296 dma_write32(val, DMA_CSR);
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;
306 val &= ~(DMA_ESC_BURST);
308 dma_write32(val, DMA_CSR);
315 /* Enable interrupts. */
316 val = dma_read32(DMA_CSR);
317 dma_write32(val | DMA_INT_ENAB, DMA_CSR);
320 static void sbus_esp_dma_drain(struct esp *esp)
325 if (esp->dmarev == dvmahme)
328 csr = dma_read32(DMA_CSR);
329 if (!(csr & DMA_FIFO_ISDRAIN))
332 if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1)
333 dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
336 while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
338 printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
339 esp->host->unique_id);
346 static void sbus_esp_dma_invalidate(struct esp *esp)
348 if (esp->dmarev == dvmahme) {
349 dma_write32(DMA_RST_SCSI, DMA_CSR);
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));
356 dma_write32(0, DMA_CSR);
357 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
359 /* This is necessary to avoid having the SCSI channel
360 * engine lock up on us.
362 dma_write32(0, DMA_ADDR);
368 while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
370 printk(KERN_ALERT PFX "esp%d: DMA will not "
371 "invalidate!\n", esp->host->unique_id);
377 val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
379 dma_write32(val, DMA_CSR);
380 val &= ~DMA_FIFO_INV;
381 dma_write32(val, DMA_CSR);
385 static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
386 u32 dma_count, int write, u8 cmd)
390 BUG_ON(!(cmd & ESP_CMD_DMA));
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);
398 scsi_esp_cmd(esp, cmd);
400 csr = esp->prev_hme_dmacsr;
401 csr |= DMA_SCSI_DISAB | DMA_ENABLE;
405 csr &= ~DMA_ST_WRITE;
406 esp->prev_hme_dmacsr = csr;
408 dma_write32(dma_count, DMA_COUNT);
409 dma_write32(addr, DMA_ADDR);
410 dma_write32(csr, DMA_CSR);
412 csr = dma_read32(DMA_CSR);
417 csr &= ~DMA_ST_WRITE;
418 dma_write32(csr, DMA_CSR);
419 if (esp->dmarev == dvmaesc1) {
420 u32 end = PAGE_ALIGN(addr + dma_count + 16U);
421 dma_write32(end - addr, DMA_COUNT);
423 dma_write32(addr, DMA_ADDR);
425 scsi_esp_cmd(esp, cmd);
430 static int sbus_esp_dma_error(struct esp *esp)
432 u32 csr = dma_read32(DMA_CSR);
434 if (csr & DMA_HNDL_ERROR)
440 static const struct esp_driver_ops sbus_esp_ops = {
441 .esp_write8 = sbus_esp_write8,
442 .esp_read8 = sbus_esp_read8,
443 .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,
451 static int esp_sbus_probe_one(struct platform_device *op,
452 struct platform_device *espdma, int hme)
454 struct scsi_host_template *tpnt = &scsi_esp_template;
455 struct Scsi_Host *host;
459 host = scsi_host_alloc(tpnt, sizeof(struct esp));
465 host->max_id = (hme ? 16 : 8);
466 esp = shost_priv(host);
470 esp->ops = &sbus_esp_ops;
473 esp->flags |= ESP_FLAG_WIDE_CAPABLE;
475 err = esp_sbus_setup_dma(esp, espdma);
479 err = esp_sbus_map_regs(esp, hme);
483 err = esp_sbus_map_command_block(esp);
485 goto fail_unmap_regs;
487 err = esp_sbus_register_irq(esp);
489 goto fail_unmap_command_block;
491 esp_sbus_get_props(esp, espdma);
493 /* Before we try to touch the ESP chip, ESC1 dma can
494 * come up with the reset bit set, so make sure that
497 if (esp->dmarev == dvmaesc1) {
498 u32 val = dma_read32(DMA_CSR);
500 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
503 dev_set_drvdata(&op->dev, esp);
505 err = scsi_esp_register(esp);
512 free_irq(host->irq, esp);
513 fail_unmap_command_block:
514 dma_free_coherent(&op->dev, 16,
516 esp->command_block_dma);
518 of_iounmap(&op->resource[(hme ? 1 : 0)], esp->regs, SBUS_ESP_REG_SIZE);
525 static int esp_sbus_probe(struct platform_device *op)
527 struct device_node *dma_node = NULL;
528 struct device_node *dp = op->dev.of_node;
529 struct platform_device *dma_of = NULL;
533 if (of_node_name_eq(dp->parent, "espdma") ||
534 of_node_name_eq(dp->parent, "dma"))
535 dma_node = dp->parent;
536 else if (of_node_name_eq(dp, "SUNW,fas")) {
537 dma_node = op->dev.of_node;
541 dma_of = of_find_device_by_node(dma_node);
545 ret = esp_sbus_probe_one(op, dma_of, hme);
547 put_device(&dma_of->dev);
552 static int esp_sbus_remove(struct platform_device *op)
554 struct esp *esp = dev_get_drvdata(&op->dev);
555 struct platform_device *dma_of = esp->dma;
556 unsigned int irq = esp->host->irq;
560 scsi_esp_unregister(esp);
562 /* Disable interrupts. */
563 val = dma_read32(DMA_CSR);
564 dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
568 is_hme = (esp->dmarev == dvmahme);
570 dma_free_coherent(&op->dev, 16,
572 esp->command_block_dma);
573 of_iounmap(&op->resource[(is_hme ? 1 : 0)], esp->regs,
575 of_iounmap(&dma_of->resource[0], esp->dma_regs,
576 resource_size(&dma_of->resource[0]));
578 scsi_host_put(esp->host);
580 dev_set_drvdata(&op->dev, NULL);
582 put_device(&dma_of->dev);
587 static const struct of_device_id esp_match[] = {
599 MODULE_DEVICE_TABLE(of, esp_match);
601 static struct platform_driver esp_sbus_driver = {
604 .of_match_table = esp_match,
606 .probe = esp_sbus_probe,
607 .remove = esp_sbus_remove,
609 module_platform_driver(esp_sbus_driver);
611 MODULE_DESCRIPTION("Sun ESP SCSI driver");
612 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
613 MODULE_LICENSE("GPL");
614 MODULE_VERSION(DRV_VERSION);