blob: 60a88a95a8e23f2add54e66bf419f757a887642b [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +02002/* jazz_esp.c: ESP front-end for MIPS JAZZ systems.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Al Virod36b6912011-12-29 17:09:01 -05004 * Copyright (C) 2007 Thomas Bogendörfer (tsbogend@alpha.frankende)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/platform_device.h>
14#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/irq.h>
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020017#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/dma.h>
19
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020020#include <asm/jazz.h>
21#include <asm/jazzdma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020023#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020025#include "esp_scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020027#define DRV_MODULE_NAME "jazz_esp"
28#define PFX DRV_MODULE_NAME ": "
29#define DRV_VERSION "1.000"
30#define DRV_MODULE_RELDATE "May 19, 2007"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020032static void jazz_esp_write8(struct esp *esp, u8 val, unsigned long reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020034 *(volatile u8 *)(esp->regs + reg) = val;
35}
36
37static u8 jazz_esp_read8(struct esp *esp, unsigned long reg)
38{
39 return *(volatile u8 *)(esp->regs + reg);
40}
41
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020042static int jazz_esp_irq_pending(struct esp *esp)
43{
44 if (jazz_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR)
45 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return 0;
47}
48
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020049static void jazz_esp_reset_dma(struct esp *esp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020051 vdma_disable ((int)esp->dma_regs);
52}
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020054static void jazz_esp_dma_drain(struct esp *esp)
55{
56 /* nothing to do */
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020059static void jazz_esp_dma_invalidate(struct esp *esp)
60{
61 vdma_disable ((int)esp->dma_regs);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020064static void jazz_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
65 u32 dma_count, int write, u8 cmd)
66{
67 BUG_ON(!(cmd & ESP_CMD_DMA));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020069 jazz_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
70 jazz_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
71 vdma_disable ((int)esp->dma_regs);
72 if (write)
73 vdma_set_mode ((int)esp->dma_regs, DMA_MODE_READ);
74 else
75 vdma_set_mode ((int)esp->dma_regs, DMA_MODE_WRITE);
76
77 vdma_set_addr ((int)esp->dma_regs, addr);
78 vdma_set_count ((int)esp->dma_regs, dma_count);
79 vdma_enable ((int)esp->dma_regs);
80
81 scsi_esp_cmd(esp, cmd);
82}
83
84static int jazz_esp_dma_error(struct esp *esp)
85{
86 u32 enable = vdma_get_enable((int)esp->dma_regs);
87
88 if (enable & (R4030_MEM_INTR|R4030_ADDR_INTR))
89 return 1;
90
91 return 0;
92}
93
94static const struct esp_driver_ops jazz_esp_ops = {
95 .esp_write8 = jazz_esp_write8,
96 .esp_read8 = jazz_esp_read8,
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +020097 .irq_pending = jazz_esp_irq_pending,
98 .reset_dma = jazz_esp_reset_dma,
99 .dma_drain = jazz_esp_dma_drain,
100 .dma_invalidate = jazz_esp_dma_invalidate,
101 .send_dma_cmd = jazz_esp_send_dma_cmd,
102 .dma_error = jazz_esp_dma_error,
103};
104
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800105static int esp_jazz_probe(struct platform_device *dev)
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200106{
107 struct scsi_host_template *tpnt = &scsi_esp_template;
108 struct Scsi_Host *host;
109 struct esp *esp;
110 struct resource *res;
111 int err;
112
113 host = scsi_host_alloc(tpnt, sizeof(struct esp));
114
115 err = -ENOMEM;
116 if (!host)
117 goto fail;
118
119 host->max_id = 8;
Christoph Hellwig2b14ec72007-05-31 20:12:32 +0200120 esp = shost_priv(host);
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200121
122 esp->host = host;
Finn Thaine32ec652018-03-07 17:56:41 +1100123 esp->dev = &dev->dev;
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200124 esp->ops = &jazz_esp_ops;
125
126 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
127 if (!res)
128 goto fail_unlink;
129
130 esp->regs = (void __iomem *)res->start;
131 if (!esp->regs)
132 goto fail_unlink;
133
134 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
135 if (!res)
136 goto fail_unlink;
137
138 esp->dma_regs = (void __iomem *)res->start;
139
140 esp->command_block = dma_alloc_coherent(esp->dev, 16,
141 &esp->command_block_dma,
142 GFP_KERNEL);
143 if (!esp->command_block)
144 goto fail_unmap_regs;
145
Sergey Shtylyov38fca152021-03-30 20:43:23 +0300146 host->irq = err = platform_get_irq(dev, 0);
147 if (err < 0)
148 goto fail_unmap_command_block;
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200149 err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
150 if (err < 0)
151 goto fail_unmap_command_block;
152
153 esp->scsi_id = 7;
154 esp->host->this_id = esp->scsi_id;
155 esp->scsi_id_mask = (1 << esp->scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 esp->cfreq = 40000000;
157
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200158 dev_set_drvdata(&dev->dev, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Christoph Hellwig44b1b4d2018-10-13 09:26:26 +0200160 err = scsi_esp_register(esp);
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200161 if (err)
162 goto fail_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200164 return 0;
165
166fail_free_irq:
167 free_irq(host->irq, esp);
168fail_unmap_command_block:
169 dma_free_coherent(esp->dev, 16,
170 esp->command_block,
171 esp->command_block_dma);
172fail_unmap_regs:
173fail_unlink:
174 scsi_host_put(host);
175fail:
176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800179static int esp_jazz_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200181 struct esp *esp = dev_get_drvdata(&dev->dev);
182 unsigned int irq = esp->host->irq;
183
184 scsi_esp_unregister(esp);
185
186 free_irq(irq, esp);
187 dma_free_coherent(esp->dev, 16,
188 esp->command_block,
189 esp->command_block_dma);
190
191 scsi_host_put(esp->host);
192
193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Kay Sieversecc1241e2008-04-18 13:57:19 -0700196/* work with hotplug and coldplug */
197MODULE_ALIAS("platform:jazz_esp");
198
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200199static struct platform_driver esp_jazz_driver = {
200 .probe = esp_jazz_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800201 .remove = esp_jazz_remove,
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200202 .driver = {
203 .name = "jazz_esp",
204 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
Liu Shixin7fc83de2020-09-14 14:54:03 +0800206module_platform_driver(esp_jazz_driver);
Thomas Bogendoerfer352e9212007-05-22 10:13:19 +0200207
208MODULE_DESCRIPTION("JAZZ ESP SCSI driver");
209MODULE_AUTHOR("Thomas Bogendoerfer (tsbogend@alpha.franken.de)");
210MODULE_LICENSE("GPL");
211MODULE_VERSION(DRV_VERSION);