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_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/gfp.h>
23 #include <scsi/scsi_host.h>
27 #define DRV_MODULE_NAME "sun_esp"
28 #define PFX DRV_MODULE_NAME ": "
29 #define DRV_VERSION "1.100"
30 #define DRV_MODULE_RELDATE "August 27, 2008"
32 #define dma_read32(REG) \
33 sbus_readl(esp->dma_regs + (REG))
34 #define dma_write32(VAL, REG) \
35 sbus_writel((VAL), esp->dma_regs + (REG))
37 /* DVMA chip revisions */
48 static int esp_sbus_setup_dma(struct esp
*esp
, struct platform_device
*dma_of
)
52 esp
->dma_regs
= of_ioremap(&dma_of
->resource
[0], 0,
53 resource_size(&dma_of
->resource
[0]),
58 switch (dma_read32(DMA_CSR
) & DMA_DEVICE_ID
) {
60 esp
->dmarev
= dvmarev0
;
63 esp
->dmarev
= dvmaesc1
;
66 esp
->dmarev
= dvmarev1
;
69 esp
->dmarev
= dvmarev2
;
72 esp
->dmarev
= dvmahme
;
75 esp
->dmarev
= dvmarevplus
;
83 static int esp_sbus_map_regs(struct esp
*esp
, int hme
)
85 struct platform_device
*op
= to_platform_device(esp
->dev
);
88 /* On HME, two reg sets exist, first is DVMA,
89 * second is ESP registers.
92 res
= &op
->resource
[1];
94 res
= &op
->resource
[0];
96 esp
->regs
= of_ioremap(res
, 0, SBUS_ESP_REG_SIZE
, "ESP");
103 static int esp_sbus_map_command_block(struct esp
*esp
)
105 esp
->command_block
= dma_alloc_coherent(esp
->dev
, 16,
106 &esp
->command_block_dma
,
108 if (!esp
->command_block
)
113 static int esp_sbus_register_irq(struct esp
*esp
)
115 struct Scsi_Host
*host
= esp
->host
;
116 struct platform_device
*op
= to_platform_device(esp
->dev
);
118 host
->irq
= op
->archdata
.irqs
[0];
119 return request_irq(host
->irq
, scsi_esp_intr
, IRQF_SHARED
, "ESP", esp
);
122 static void esp_get_scsi_id(struct esp
*esp
, struct platform_device
*espdma
)
124 struct platform_device
*op
= to_platform_device(esp
->dev
);
125 struct device_node
*dp
;
127 dp
= op
->dev
.of_node
;
128 esp
->scsi_id
= of_getintprop_default(dp
, "initiator-id", 0xff);
129 if (esp
->scsi_id
!= 0xff)
132 esp
->scsi_id
= of_getintprop_default(dp
, "scsi-initiator-id", 0xff);
133 if (esp
->scsi_id
!= 0xff)
136 esp
->scsi_id
= of_getintprop_default(espdma
->dev
.of_node
,
137 "scsi-initiator-id", 7);
140 esp
->host
->this_id
= esp
->scsi_id
;
141 esp
->scsi_id_mask
= (1 << esp
->scsi_id
);
144 static void esp_get_differential(struct esp
*esp
)
146 struct platform_device
*op
= to_platform_device(esp
->dev
);
147 struct device_node
*dp
;
149 dp
= op
->dev
.of_node
;
150 if (of_property_read_bool(dp
, "differential"))
151 esp
->flags
|= ESP_FLAG_DIFFERENTIAL
;
153 esp
->flags
&= ~ESP_FLAG_DIFFERENTIAL
;
156 static void esp_get_clock_params(struct esp
*esp
)
158 struct platform_device
*op
= to_platform_device(esp
->dev
);
159 struct device_node
*bus_dp
, *dp
;
162 dp
= op
->dev
.of_node
;
165 fmhz
= of_getintprop_default(dp
, "clock-frequency", 0);
167 fmhz
= of_getintprop_default(bus_dp
, "clock-frequency", 0);
172 static void esp_get_bursts(struct esp
*esp
, struct platform_device
*dma_of
)
174 struct device_node
*dma_dp
= dma_of
->dev
.of_node
;
175 struct platform_device
*op
= to_platform_device(esp
->dev
);
176 struct device_node
*dp
;
179 dp
= op
->dev
.of_node
;
180 bursts
= of_getintprop_default(dp
, "burst-sizes", 0xff);
181 val
= of_getintprop_default(dma_dp
, "burst-sizes", 0xff);
185 val
= of_getintprop_default(dma_dp
->parent
, "burst-sizes", 0xff);
189 if (bursts
== 0xff ||
190 (bursts
& DMA_BURST16
) == 0 ||
191 (bursts
& DMA_BURST32
) == 0)
192 bursts
= (DMA_BURST32
- 1);
194 esp
->bursts
= bursts
;
197 static void esp_sbus_get_props(struct esp
*esp
, struct platform_device
*espdma
)
199 esp_get_scsi_id(esp
, espdma
);
200 esp_get_differential(esp
);
201 esp_get_clock_params(esp
);
202 esp_get_bursts(esp
, espdma
);
205 static void sbus_esp_write8(struct esp
*esp
, u8 val
, unsigned long reg
)
207 sbus_writeb(val
, esp
->regs
+ (reg
* 4UL));
210 static u8
sbus_esp_read8(struct esp
*esp
, unsigned long reg
)
212 return sbus_readb(esp
->regs
+ (reg
* 4UL));
215 static int sbus_esp_irq_pending(struct esp
*esp
)
217 if (dma_read32(DMA_CSR
) & (DMA_HNDL_INTR
| DMA_HNDL_ERROR
))
222 static void sbus_esp_reset_dma(struct esp
*esp
)
224 int can_do_burst16
, can_do_burst32
, can_do_burst64
;
225 int can_do_sbus64
, lim
;
226 struct platform_device
*op
= to_platform_device(esp
->dev
);
229 can_do_burst16
= (esp
->bursts
& DMA_BURST16
) != 0;
230 can_do_burst32
= (esp
->bursts
& DMA_BURST32
) != 0;
233 if (sbus_can_dma_64bit())
235 if (sbus_can_burst64())
236 can_do_burst64
= (esp
->bursts
& DMA_BURST64
) != 0;
238 /* Put the DVMA into a known state. */
239 if (esp
->dmarev
!= dvmahme
) {
240 val
= dma_read32(DMA_CSR
);
241 dma_write32(val
| DMA_RST_SCSI
, DMA_CSR
);
242 dma_write32(val
& ~DMA_RST_SCSI
, DMA_CSR
);
244 switch (esp
->dmarev
) {
246 dma_write32(DMA_RESET_FAS366
, DMA_CSR
);
247 dma_write32(DMA_RST_SCSI
, DMA_CSR
);
249 esp
->prev_hme_dmacsr
= (DMA_PARITY_OFF
| DMA_2CLKS
|
250 DMA_SCSI_DISAB
| DMA_INT_ENAB
);
252 esp
->prev_hme_dmacsr
&= ~(DMA_ENABLE
| DMA_ST_WRITE
|
256 esp
->prev_hme_dmacsr
|= DMA_BRST64
;
257 else if (can_do_burst32
)
258 esp
->prev_hme_dmacsr
|= DMA_BRST32
;
261 esp
->prev_hme_dmacsr
|= DMA_SCSI_SBUS64
;
262 sbus_set_sbus64(&op
->dev
, esp
->bursts
);
266 while (dma_read32(DMA_CSR
) & DMA_PEND_READ
) {
268 printk(KERN_ALERT PFX
"esp%d: DMA_PEND_READ "
270 esp
->host
->unique_id
);
276 dma_write32(0, DMA_CSR
);
277 dma_write32(esp
->prev_hme_dmacsr
, DMA_CSR
);
279 dma_write32(0, DMA_ADDR
);
283 if (esp
->rev
!= ESP100
) {
284 val
= dma_read32(DMA_CSR
);
285 dma_write32(val
| DMA_3CLKS
, DMA_CSR
);
290 val
= dma_read32(DMA_CSR
);
293 if (can_do_burst32
) {
297 dma_write32(val
, DMA_CSR
);
301 val
= dma_read32(DMA_CSR
);
302 val
|= DMA_ADD_ENABLE
;
303 val
&= ~DMA_BCNT_ENAB
;
304 if (!can_do_burst32
&& can_do_burst16
) {
305 val
|= DMA_ESC_BURST
;
307 val
&= ~(DMA_ESC_BURST
);
309 dma_write32(val
, DMA_CSR
);
316 /* Enable interrupts. */
317 val
= dma_read32(DMA_CSR
);
318 dma_write32(val
| DMA_INT_ENAB
, DMA_CSR
);
321 static void sbus_esp_dma_drain(struct esp
*esp
)
326 if (esp
->dmarev
== dvmahme
)
329 csr
= dma_read32(DMA_CSR
);
330 if (!(csr
& DMA_FIFO_ISDRAIN
))
333 if (esp
->dmarev
!= dvmarev3
&& esp
->dmarev
!= dvmaesc1
)
334 dma_write32(csr
| DMA_FIFO_STDRAIN
, DMA_CSR
);
337 while (dma_read32(DMA_CSR
) & DMA_FIFO_ISDRAIN
) {
339 printk(KERN_ALERT PFX
"esp%d: DMA will not drain!\n",
340 esp
->host
->unique_id
);
347 static void sbus_esp_dma_invalidate(struct esp
*esp
)
349 if (esp
->dmarev
== dvmahme
) {
350 dma_write32(DMA_RST_SCSI
, DMA_CSR
);
352 esp
->prev_hme_dmacsr
= ((esp
->prev_hme_dmacsr
|
353 (DMA_PARITY_OFF
| DMA_2CLKS
|
354 DMA_SCSI_DISAB
| DMA_INT_ENAB
)) &
355 ~(DMA_ST_WRITE
| DMA_ENABLE
));
357 dma_write32(0, DMA_CSR
);
358 dma_write32(esp
->prev_hme_dmacsr
, DMA_CSR
);
360 /* This is necessary to avoid having the SCSI channel
361 * engine lock up on us.
363 dma_write32(0, DMA_ADDR
);
369 while ((val
= dma_read32(DMA_CSR
)) & DMA_PEND_READ
) {
371 printk(KERN_ALERT PFX
"esp%d: DMA will not "
372 "invalidate!\n", esp
->host
->unique_id
);
378 val
&= ~(DMA_ENABLE
| DMA_ST_WRITE
| DMA_BCNT_ENAB
);
380 dma_write32(val
, DMA_CSR
);
381 val
&= ~DMA_FIFO_INV
;
382 dma_write32(val
, DMA_CSR
);
386 static void sbus_esp_send_dma_cmd(struct esp
*esp
, u32 addr
, u32 esp_count
,
387 u32 dma_count
, int write
, u8 cmd
)
391 BUG_ON(!(cmd
& ESP_CMD_DMA
));
393 sbus_esp_write8(esp
, (esp_count
>> 0) & 0xff, ESP_TCLOW
);
394 sbus_esp_write8(esp
, (esp_count
>> 8) & 0xff, ESP_TCMED
);
395 if (esp
->rev
== FASHME
) {
396 sbus_esp_write8(esp
, (esp_count
>> 16) & 0xff, FAS_RLO
);
397 sbus_esp_write8(esp
, 0, FAS_RHI
);
399 scsi_esp_cmd(esp
, cmd
);
401 csr
= esp
->prev_hme_dmacsr
;
402 csr
|= DMA_SCSI_DISAB
| DMA_ENABLE
;
406 csr
&= ~DMA_ST_WRITE
;
407 esp
->prev_hme_dmacsr
= csr
;
409 dma_write32(dma_count
, DMA_COUNT
);
410 dma_write32(addr
, DMA_ADDR
);
411 dma_write32(csr
, DMA_CSR
);
413 csr
= dma_read32(DMA_CSR
);
418 csr
&= ~DMA_ST_WRITE
;
419 dma_write32(csr
, DMA_CSR
);
420 if (esp
->dmarev
== dvmaesc1
) {
421 u32 end
= PAGE_ALIGN(addr
+ dma_count
+ 16U);
422 dma_write32(end
- addr
, DMA_COUNT
);
424 dma_write32(addr
, DMA_ADDR
);
426 scsi_esp_cmd(esp
, cmd
);
431 static int sbus_esp_dma_error(struct esp
*esp
)
433 u32 csr
= dma_read32(DMA_CSR
);
435 if (csr
& DMA_HNDL_ERROR
)
441 static const struct esp_driver_ops sbus_esp_ops
= {
442 .esp_write8
= sbus_esp_write8
,
443 .esp_read8
= sbus_esp_read8
,
444 .irq_pending
= sbus_esp_irq_pending
,
445 .reset_dma
= sbus_esp_reset_dma
,
446 .dma_drain
= sbus_esp_dma_drain
,
447 .dma_invalidate
= sbus_esp_dma_invalidate
,
448 .send_dma_cmd
= sbus_esp_send_dma_cmd
,
449 .dma_error
= sbus_esp_dma_error
,
452 static int esp_sbus_probe_one(struct platform_device
*op
,
453 struct platform_device
*espdma
, int hme
)
455 const struct scsi_host_template
*tpnt
= &scsi_esp_template
;
456 struct Scsi_Host
*host
;
460 host
= scsi_host_alloc(tpnt
, sizeof(struct esp
));
466 host
->max_id
= (hme
? 16 : 8);
467 esp
= shost_priv(host
);
471 esp
->ops
= &sbus_esp_ops
;
474 esp
->flags
|= ESP_FLAG_WIDE_CAPABLE
;
476 err
= esp_sbus_setup_dma(esp
, espdma
);
480 err
= esp_sbus_map_regs(esp
, hme
);
484 err
= esp_sbus_map_command_block(esp
);
486 goto fail_unmap_regs
;
488 err
= esp_sbus_register_irq(esp
);
490 goto fail_unmap_command_block
;
492 esp_sbus_get_props(esp
, espdma
);
494 /* Before we try to touch the ESP chip, ESC1 dma can
495 * come up with the reset bit set, so make sure that
498 if (esp
->dmarev
== dvmaesc1
) {
499 u32 val
= dma_read32(DMA_CSR
);
501 dma_write32(val
& ~DMA_RST_SCSI
, DMA_CSR
);
504 dev_set_drvdata(&op
->dev
, esp
);
506 err
= scsi_esp_register(esp
);
513 free_irq(host
->irq
, esp
);
514 fail_unmap_command_block
:
515 dma_free_coherent(&op
->dev
, 16,
517 esp
->command_block_dma
);
519 of_iounmap(&op
->resource
[(hme
? 1 : 0)], esp
->regs
, SBUS_ESP_REG_SIZE
);
526 static int esp_sbus_probe(struct platform_device
*op
)
528 struct device_node
*dma_node
= NULL
;
529 struct device_node
*dp
= op
->dev
.of_node
;
530 struct platform_device
*dma_of
= NULL
;
534 if (of_node_name_eq(dp
->parent
, "espdma") ||
535 of_node_name_eq(dp
->parent
, "dma"))
536 dma_node
= dp
->parent
;
537 else if (of_node_name_eq(dp
, "SUNW,fas")) {
538 dma_node
= op
->dev
.of_node
;
542 dma_of
= of_find_device_by_node(dma_node
);
546 ret
= esp_sbus_probe_one(op
, dma_of
, hme
);
548 put_device(&dma_of
->dev
);
553 static void esp_sbus_remove(struct platform_device
*op
)
555 struct esp
*esp
= dev_get_drvdata(&op
->dev
);
556 struct platform_device
*dma_of
= esp
->dma
;
557 unsigned int irq
= esp
->host
->irq
;
561 scsi_esp_unregister(esp
);
563 /* Disable interrupts. */
564 val
= dma_read32(DMA_CSR
);
565 dma_write32(val
& ~DMA_INT_ENAB
, DMA_CSR
);
569 is_hme
= (esp
->dmarev
== dvmahme
);
571 dma_free_coherent(&op
->dev
, 16,
573 esp
->command_block_dma
);
574 of_iounmap(&op
->resource
[(is_hme
? 1 : 0)], esp
->regs
,
576 of_iounmap(&dma_of
->resource
[0], esp
->dma_regs
,
577 resource_size(&dma_of
->resource
[0]));
579 scsi_host_put(esp
->host
);
581 dev_set_drvdata(&op
->dev
, NULL
);
583 put_device(&dma_of
->dev
);
586 static const struct of_device_id esp_match
[] = {
598 MODULE_DEVICE_TABLE(of
, esp_match
);
600 static struct platform_driver esp_sbus_driver
= {
603 .of_match_table
= esp_match
,
605 .probe
= esp_sbus_probe
,
606 .remove_new
= esp_sbus_remove
,
608 module_platform_driver(esp_sbus_driver
);
610 MODULE_DESCRIPTION("Sun ESP SCSI driver");
611 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
612 MODULE_LICENSE("GPL");
613 MODULE_VERSION(DRV_VERSION
);