1 /* sun_esp.c: ESP front-end for Sparc SBUS systems.
3 * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/delay.h>
9 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/dma-mapping.h>
20 #include <scsi/scsi_host.h>
24 #define DRV_MODULE_NAME "sun_esp"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_VERSION "1.000"
27 #define DRV_MODULE_RELDATE "April 19, 2007"
29 #define dma_read32(REG) \
30 sbus_readl(esp->dma_regs + (REG))
31 #define dma_write32(VAL, REG) \
32 sbus_writel((VAL), esp->dma_regs + (REG))
34 /* DVMA chip revisions */
45 static int __devinit esp_sbus_setup_dma(struct esp *esp,
46 struct of_device *dma_of)
50 esp->dma_regs = of_ioremap(&dma_of->resource[0], 0,
51 resource_size(&dma_of->resource[0]),
56 switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) {
58 esp->dmarev = dvmarev0;
61 esp->dmarev = dvmaesc1;
64 esp->dmarev = dvmarev1;
67 esp->dmarev = dvmarev2;
70 esp->dmarev = dvmahme;
73 esp->dmarev = dvmarevplus;
81 static int __devinit esp_sbus_map_regs(struct esp *esp, int hme)
83 struct sbus_dev *sdev = esp->dev;
86 /* On HME, two reg sets exist, first is DVMA,
87 * second is ESP registers.
90 res = &sdev->resource[1];
92 res = &sdev->resource[0];
94 esp->regs = sbus_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP");
101 static int __devinit esp_sbus_map_command_block(struct esp *esp)
103 struct sbus_dev *sdev = esp->dev;
105 esp->command_block = dma_alloc_coherent(&sdev->ofdev.dev, 16,
106 &esp->command_block_dma,
108 if (!esp->command_block)
113 static int __devinit esp_sbus_register_irq(struct esp *esp)
115 struct Scsi_Host *host = esp->host;
116 struct sbus_dev *sdev = esp->dev;
118 host->irq = sdev->irqs[0];
119 return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
122 static void __devinit esp_get_scsi_id(struct esp *esp)
124 struct sbus_dev *sdev = esp->dev;
125 struct device_node *dp = sdev->ofdev.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)
141 esp->scsi_id = of_getintprop_default(sdev->bus->ofdev.node,
142 "scsi-initiator-id", 7);
145 esp->host->this_id = esp->scsi_id;
146 esp->scsi_id_mask = (1 << esp->scsi_id);
149 static void __devinit esp_get_differential(struct esp *esp)
151 struct sbus_dev *sdev = esp->dev;
152 struct device_node *dp = sdev->ofdev.node;
154 if (of_find_property(dp, "differential", NULL))
155 esp->flags |= ESP_FLAG_DIFFERENTIAL;
157 esp->flags &= ~ESP_FLAG_DIFFERENTIAL;
160 static void __devinit esp_get_clock_params(struct esp *esp)
162 struct sbus_dev *sdev = esp->dev;
163 struct device_node *dp = sdev->ofdev.node;
164 struct device_node *bus_dp;
168 if (sdev != NULL && sdev->bus != NULL)
169 bus_dp = sdev->bus->ofdev.node;
171 fmhz = of_getintprop_default(dp, "clock-frequency", 0);
173 fmhz = (!bus_dp) ? 0 :
174 of_getintprop_default(bus_dp, "clock-frequency", 0);
179 static void __devinit esp_get_bursts(struct esp *esp, struct of_device *dma_of)
181 struct device_node *dma_dp = dma_of->node;
182 struct sbus_dev *sdev = esp->dev;
183 struct device_node *dp;
186 dp = sdev->ofdev.node;
187 bursts = of_getintprop_default(dp, "burst-sizes", 0xff);
188 val = of_getintprop_default(dma_dp, "burst-sizes", 0xff);
193 u8 val = of_getintprop_default(sdev->bus->ofdev.node,
194 "burst-sizes", 0xff);
199 if (bursts == 0xff ||
200 (bursts & DMA_BURST16) == 0 ||
201 (bursts & DMA_BURST32) == 0)
202 bursts = (DMA_BURST32 - 1);
204 esp->bursts = bursts;
207 static void __devinit esp_sbus_get_props(struct esp *esp, struct of_device *espdma)
209 esp_get_scsi_id(esp);
210 esp_get_differential(esp);
211 esp_get_clock_params(esp);
212 esp_get_bursts(esp, espdma);
215 static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg)
217 sbus_writeb(val, esp->regs + (reg * 4UL));
220 static u8 sbus_esp_read8(struct esp *esp, unsigned long reg)
222 return sbus_readb(esp->regs + (reg * 4UL));
225 static dma_addr_t sbus_esp_map_single(struct esp *esp, void *buf,
228 struct sbus_dev *sdev = esp->dev;
230 return dma_map_single(&sdev->ofdev.dev, buf, sz, dir);
233 static int sbus_esp_map_sg(struct esp *esp, struct scatterlist *sg,
236 struct sbus_dev *sdev = esp->dev;
238 return dma_map_sg(&sdev->ofdev.dev, sg, num_sg, dir);
241 static void sbus_esp_unmap_single(struct esp *esp, dma_addr_t addr,
244 struct sbus_dev *sdev = esp->dev;
246 dma_unmap_single(&sdev->ofdev.dev, addr, sz, dir);
249 static void sbus_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
252 struct sbus_dev *sdev = esp->dev;
254 dma_unmap_sg(&sdev->ofdev.dev, sg, num_sg, dir);
257 static int sbus_esp_irq_pending(struct esp *esp)
259 if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
264 static void sbus_esp_reset_dma(struct esp *esp)
266 int can_do_burst16, can_do_burst32, can_do_burst64;
267 int can_do_sbus64, lim;
270 can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
271 can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
274 if (sbus_can_dma_64bit(esp->dev))
276 if (sbus_can_burst64(esp->sdev))
277 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
279 /* Put the DVMA into a known state. */
280 if (esp->dmarev != dvmahme) {
281 val = dma_read32(DMA_CSR);
282 dma_write32(val | DMA_RST_SCSI, DMA_CSR);
283 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
285 switch (esp->dmarev) {
287 dma_write32(DMA_RESET_FAS366, DMA_CSR);
288 dma_write32(DMA_RST_SCSI, DMA_CSR);
290 esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS |
291 DMA_SCSI_DISAB | DMA_INT_ENAB);
293 esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE |
297 esp->prev_hme_dmacsr |= DMA_BRST64;
298 else if (can_do_burst32)
299 esp->prev_hme_dmacsr |= DMA_BRST32;
302 esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
303 sbus_set_sbus64(esp->dev, esp->bursts);
307 while (dma_read32(DMA_CSR) & DMA_PEND_READ) {
309 printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ "
311 esp->host->unique_id);
317 dma_write32(0, DMA_CSR);
318 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
320 dma_write32(0, DMA_ADDR);
324 if (esp->rev != ESP100) {
325 val = dma_read32(DMA_CSR);
326 dma_write32(val | DMA_3CLKS, DMA_CSR);
331 val = dma_read32(DMA_CSR);
334 if (can_do_burst32) {
338 dma_write32(val, DMA_CSR);
342 val = dma_read32(DMA_CSR);
343 val |= DMA_ADD_ENABLE;
344 val &= ~DMA_BCNT_ENAB;
345 if (!can_do_burst32 && can_do_burst16) {
346 val |= DMA_ESC_BURST;
348 val &= ~(DMA_ESC_BURST);
350 dma_write32(val, DMA_CSR);
357 /* Enable interrupts. */
358 val = dma_read32(DMA_CSR);
359 dma_write32(val | DMA_INT_ENAB, DMA_CSR);
362 static void sbus_esp_dma_drain(struct esp *esp)
367 if (esp->dmarev == dvmahme)
370 csr = dma_read32(DMA_CSR);
371 if (!(csr & DMA_FIFO_ISDRAIN))
374 if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1)
375 dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
378 while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
380 printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
381 esp->host->unique_id);
388 static void sbus_esp_dma_invalidate(struct esp *esp)
390 if (esp->dmarev == dvmahme) {
391 dma_write32(DMA_RST_SCSI, DMA_CSR);
393 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
394 (DMA_PARITY_OFF | DMA_2CLKS |
395 DMA_SCSI_DISAB | DMA_INT_ENAB)) &
396 ~(DMA_ST_WRITE | DMA_ENABLE));
398 dma_write32(0, DMA_CSR);
399 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
401 /* This is necessary to avoid having the SCSI channel
402 * engine lock up on us.
404 dma_write32(0, DMA_ADDR);
410 while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
412 printk(KERN_ALERT PFX "esp%d: DMA will not "
413 "invalidate!\n", esp->host->unique_id);
419 val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
421 dma_write32(val, DMA_CSR);
422 val &= ~DMA_FIFO_INV;
423 dma_write32(val, DMA_CSR);
427 static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
428 u32 dma_count, int write, u8 cmd)
432 BUG_ON(!(cmd & ESP_CMD_DMA));
434 sbus_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
435 sbus_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
436 if (esp->rev == FASHME) {
437 sbus_esp_write8(esp, (esp_count >> 16) & 0xff, FAS_RLO);
438 sbus_esp_write8(esp, 0, FAS_RHI);
440 scsi_esp_cmd(esp, cmd);
442 csr = esp->prev_hme_dmacsr;
443 csr |= DMA_SCSI_DISAB | DMA_ENABLE;
447 csr &= ~DMA_ST_WRITE;
448 esp->prev_hme_dmacsr = csr;
450 dma_write32(dma_count, DMA_COUNT);
451 dma_write32(addr, DMA_ADDR);
452 dma_write32(csr, DMA_CSR);
454 csr = dma_read32(DMA_CSR);
459 csr &= ~DMA_ST_WRITE;
460 dma_write32(csr, DMA_CSR);
461 if (esp->dmarev == dvmaesc1) {
462 u32 end = PAGE_ALIGN(addr + dma_count + 16U);
463 dma_write32(end - addr, DMA_COUNT);
465 dma_write32(addr, DMA_ADDR);
467 scsi_esp_cmd(esp, cmd);
472 static int sbus_esp_dma_error(struct esp *esp)
474 u32 csr = dma_read32(DMA_CSR);
476 if (csr & DMA_HNDL_ERROR)
482 static const struct esp_driver_ops sbus_esp_ops = {
483 .esp_write8 = sbus_esp_write8,
484 .esp_read8 = sbus_esp_read8,
485 .map_single = sbus_esp_map_single,
486 .map_sg = sbus_esp_map_sg,
487 .unmap_single = sbus_esp_unmap_single,
488 .unmap_sg = sbus_esp_unmap_sg,
489 .irq_pending = sbus_esp_irq_pending,
490 .reset_dma = sbus_esp_reset_dma,
491 .dma_drain = sbus_esp_dma_drain,
492 .dma_invalidate = sbus_esp_dma_invalidate,
493 .send_dma_cmd = sbus_esp_send_dma_cmd,
494 .dma_error = sbus_esp_dma_error,
497 static int __devinit esp_sbus_probe_one(struct device *dev,
498 struct sbus_dev *esp_dev,
499 struct of_device *espdma,
500 struct sbus_bus *sbus,
503 struct scsi_host_template *tpnt = &scsi_esp_template;
504 struct Scsi_Host *host;
508 host = scsi_host_alloc(tpnt, sizeof(struct esp));
514 host->max_id = (hme ? 16 : 8);
515 esp = shost_priv(host);
519 esp->ops = &sbus_esp_ops;
522 esp->flags |= ESP_FLAG_WIDE_CAPABLE;
524 err = esp_sbus_setup_dma(esp, espdma);
528 err = esp_sbus_map_regs(esp, hme);
532 err = esp_sbus_map_command_block(esp);
534 goto fail_unmap_regs;
536 err = esp_sbus_register_irq(esp);
538 goto fail_unmap_command_block;
540 esp_sbus_get_props(esp, espdma);
542 /* Before we try to touch the ESP chip, ESC1 dma can
543 * come up with the reset bit set, so make sure that
546 if (esp->dmarev == dvmaesc1) {
547 u32 val = dma_read32(DMA_CSR);
549 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
552 dev_set_drvdata(&esp_dev->ofdev.dev, esp);
554 err = scsi_esp_register(esp, dev);
561 free_irq(host->irq, esp);
562 fail_unmap_command_block:
563 dma_free_coherent(&esp_dev->ofdev.dev, 16,
565 esp->command_block_dma);
567 sbus_iounmap(esp->regs, SBUS_ESP_REG_SIZE);
574 static int __devinit esp_sbus_probe(struct of_device *dev, const struct of_device_id *match)
576 struct sbus_dev *sdev = to_sbus_device(&dev->dev);
577 struct device_node *dma_node = NULL;
578 struct device_node *dp = dev->node;
579 struct of_device *dma_of = NULL;
583 (!strcmp(dp->parent->name, "espdma") ||
584 !strcmp(dp->parent->name, "dma")))
585 dma_node = dp->parent;
586 else if (!strcmp(dp->name, "SUNW,fas")) {
587 dma_node = sdev->ofdev.node;
591 dma_of = of_find_device_by_node(dma_node);
595 return esp_sbus_probe_one(&dev->dev, sdev, dma_of,
599 static int __devexit esp_sbus_remove(struct of_device *dev)
601 struct esp *esp = dev_get_drvdata(&dev->dev);
602 struct sbus_dev *sdev = esp->dev;
603 struct of_device *dma_of = esp->dma;
604 unsigned int irq = esp->host->irq;
607 scsi_esp_unregister(esp);
609 /* Disable interrupts. */
610 val = dma_read32(DMA_CSR);
611 dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
614 dma_free_coherent(&sdev->ofdev.dev, 16,
616 esp->command_block_dma);
617 sbus_iounmap(esp->regs, SBUS_ESP_REG_SIZE);
618 of_iounmap(&dma_of->resource[0], esp->dma_regs,
619 resource_size(&dma_of->resource[0]));
621 scsi_host_put(esp->host);
626 static struct of_device_id esp_match[] = {
638 MODULE_DEVICE_TABLE(of, esp_match);
640 static struct of_platform_driver esp_sbus_driver = {
642 .match_table = esp_match,
643 .probe = esp_sbus_probe,
644 .remove = __devexit_p(esp_sbus_remove),
647 static int __init sunesp_init(void)
649 return of_register_driver(&esp_sbus_driver, &sbus_bus_type);
652 static void __exit sunesp_exit(void)
654 of_unregister_driver(&esp_sbus_driver);
657 MODULE_DESCRIPTION("Sun ESP SCSI driver");
658 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
659 MODULE_LICENSE("GPL");
660 MODULE_VERSION(DRV_VERSION);
662 module_init(sunesp_init);
663 module_exit(sunesp_exit);