blob: c3028726bbe4651e30fbdccbd33f5548765892f2 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
3#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/ioport.h>
5#include <linux/init.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02006#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/spinlock.h>
8#include <linux/interrupt.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02009#include <linux/platform_device.h>
Michael Schmitze2148062022-06-30 15:33:00 +120010#include <linux/dma-mapping.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/amigaints.h>
15#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bart Van Assche53555fb2022-02-18 11:50:34 -080017#include <scsi/scsi.h>
18#include <scsi/scsi_cmnd.h>
19#include <scsi/scsi_device.h>
20#include <scsi/scsi_eh.h>
21#include <scsi/scsi_tcq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "wd33c93.h"
23#include "a3000.h"
24
Adrian Bunk9387edb2009-03-04 12:06:08 -080025
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020026struct a3000_hostdata {
27 struct WD33C93_hostdata wh;
28 struct a3000_scsiregs *regs;
Michael Schmitze2148062022-06-30 15:33:00 +120029 struct device *dev;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020030};
31
Michael Schmitze2148062022-06-30 15:33:00 +120032#define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
33
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020034static irqreturn_t a3000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020036 struct Scsi_Host *instance = data;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020037 struct a3000_hostdata *hdata = shost_priv(instance);
38 unsigned int status = hdata->regs->ISTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (!(status & ISTR_INT_P))
42 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020043 if (status & ISTR_INTS) {
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020044 spin_lock_irqsave(instance->host_lock, flags);
45 wd33c93_intr(instance);
46 spin_unlock_irqrestore(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return IRQ_HANDLED;
48 }
Kefeng Wanga2cc7012019-10-18 11:18:38 +080049 pr_warn("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return IRQ_NONE;
51}
52
Henrik Kretzschmar65396412006-09-12 23:49:33 +020053static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Bart Van Asschedbb2da52022-02-18 11:51:15 -080055 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
Michael Schmitze2148062022-06-30 15:33:00 +120056 unsigned long len = scsi_pointer->this_residual;
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020057 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020058 struct a3000_hostdata *hdata = shost_priv(instance);
59 struct WD33C93_hostdata *wh = &hdata->wh;
60 struct a3000_scsiregs *regs = hdata->regs;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020061 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
Michael Schmitze2148062022-06-30 15:33:00 +120062 dma_addr_t addr;
63
64 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
65 len, DMA_DIR(dir_in));
66 if (dma_mapping_error(hdata->dev, addr)) {
67 dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
68 scsi_pointer->ptr);
69 return 1;
70 }
71 scsi_pointer->dma_handle = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Geert Uytterhoeven21351012010-04-04 11:00:35 +020073 /*
74 * if the physical address has the wrong alignment, or if
75 * physical address is bad, or if it is a write and at the
76 * end of a physical memory chunk, then allocate a bounce
77 * buffer
Michael Schmitze2148062022-06-30 15:33:00 +120078 * MSch 20220629 - only wrong alignment tested - bounce
79 * buffer returned by kmalloc is guaranteed to be aligned
Geert Uytterhoeven21351012010-04-04 11:00:35 +020080 */
81 if (addr & A3000_XFER_MASK) {
Michael Schmitze2148062022-06-30 15:33:00 +120082 WARN_ONCE(1, "Invalid alignment for DMA!");
83 /* drop useless mapping */
84 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
85 scsi_pointer->this_residual,
86 DMA_DIR(dir_in));
87
Bart Van Asschedbb2da52022-02-18 11:51:15 -080088 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020089 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
90 GFP_KERNEL);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020091
92 /* can't allocate memory; use PIO */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020093 if (!wh->dma_bounce_buffer) {
94 wh->dma_bounce_len = 0;
Michael Schmitze2148062022-06-30 15:33:00 +120095 scsi_pointer->dma_handle = (dma_addr_t) NULL;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020096 return 1;
97 }
98
99 if (!dir_in) {
100 /* copy to bounce buffer for a write */
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800101 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
102 scsi_pointer->this_residual);
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200103 }
104
Michael Schmitze2148062022-06-30 15:33:00 +1200105 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
106 len, DMA_DIR(dir_in));
107 if (dma_mapping_error(hdata->dev, addr)) {
108 dev_warn(hdata->dev,
109 "cannot map SCSI data block %p\n",
110 scsi_pointer->ptr);
111 return 1;
112 }
113 scsi_pointer->dma_handle = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200116 /* setup dma direction */
117 if (!dir_in)
118 cntr |= CNTR_DDIR;
119
120 /* remember direction */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200121 wh->dma_dir = dir_in;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200122
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200123 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200124
125 /* setup DMA *physical* address */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200126 regs->ACR = addr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200127
Michael Schmitze2148062022-06-30 15:33:00 +1200128 /* no more cache flush here - dma_map_single() takes care */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200130 /* start DMA */
131 mb(); /* make sure setup is completed */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200132 regs->ST_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200133 mb(); /* make sure DMA has started before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200135 /* return success */
136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200139static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
140 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800142 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200143 struct a3000_hostdata *hdata = shost_priv(instance);
144 struct WD33C93_hostdata *wh = &hdata->wh;
145 struct a3000_scsiregs *regs = hdata->regs;
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200146
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200147 /* disable SCSI interrupts */
148 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200150 if (!wh->dma_dir)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200151 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200153 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200154 mb(); /* make sure CNTR is updated before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200156 /* flush if we were reading */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200157 if (wh->dma_dir) {
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200158 regs->FLUSH = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200159 mb(); /* don't allow prefetch */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200160 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200161 barrier();
162 mb(); /* no IO until FLUSH is done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200164
165 /* clear a possible interrupt */
166 /* I think that this CINT is only necessary if you are
167 * using the terminal count features. HM 7 Mar 1994
168 */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200169 regs->CINT = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200170
171 /* stop DMA */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200172 regs->SP_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200173 mb(); /* make sure DMA is stopped before next IO */
174
175 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200176 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200177 mb(); /* make sure CNTR is updated before next IO */
178
Michael Schmitze2148062022-06-30 15:33:00 +1200179 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
180 scsi_pointer->this_residual,
181 DMA_DIR(wh->dma_dir));
182
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200183 /* copy from a bounce buffer, if necessary */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200184 if (status && wh->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200185 if (SCpnt) {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200186 if (wh->dma_dir && SCpnt)
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800187 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
188 scsi_pointer->this_residual);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200189 kfree(wh->dma_bounce_buffer);
190 wh->dma_bounce_buffer = NULL;
191 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200192 } else {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200193 kfree(wh->dma_bounce_buffer);
194 wh->dma_bounce_buffer = NULL;
195 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200196 }
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Bart Van Assche88530b32023-03-22 12:54:10 -0700200static const struct scsi_host_template amiga_a3000_scsi_template = {
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200201 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .name = "Amiga 3000 built-in SCSI",
Al Viro408bb252013-03-31 00:30:35 -0400203 .show_info = wd33c93_show_info,
204 .write_info = wd33c93_write_info,
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200205 .proc_name = "A3000",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .queuecommand = wd33c93_queuecommand,
207 .eh_abort_handler = wd33c93_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .eh_host_reset_handler = wd33c93_host_reset,
209 .can_queue = CAN_QUEUE,
210 .this_id = 7,
211 .sg_tablesize = SG_ALL,
212 .cmd_per_lun = CMD_PER_LUN,
Bart Van Asschedbb2da52022-02-18 11:51:15 -0800213 .cmd_size = sizeof(struct scsi_pointer),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200216static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200218 struct resource *res;
219 struct Scsi_Host *instance;
220 int error;
221 struct a3000_scsiregs *regs;
222 wd33c93_regs wdregs;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200223 struct a3000_hostdata *hdata;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200224
Michael Schmitze2148062022-06-30 15:33:00 +1200225 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
226 dev_warn(&pdev->dev, "cannot use 32 bit DMA\n");
227 return -ENODEV;
228 }
229
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 if (!res)
232 return -ENODEV;
233
234 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
235 return -EBUSY;
236
237 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200238 sizeof(struct a3000_hostdata));
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200239 if (!instance) {
240 error = -ENOMEM;
241 goto fail_alloc;
242 }
243
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200244 instance->irq = IRQ_AMIGA_PORTS;
245
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +0100246 regs = ZTWO_VADDR(res->start);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200247 regs->DAWR = DAWR_A3000;
248
249 wdregs.SASR = &regs->SASR;
250 wdregs.SCMD = &regs->SCMD;
251
252 hdata = shost_priv(instance);
Michael Schmitze2148062022-06-30 15:33:00 +1200253 hdata->dev = &pdev->dev;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200254 hdata->wh.no_sync = 0xff;
255 hdata->wh.fast = 0;
256 hdata->wh.dma_mode = CTRL_DMA;
257 hdata->regs = regs;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200258
259 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
260 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
261 "A3000 SCSI", instance);
262 if (error)
263 goto fail_irq;
264
265 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
266
267 error = scsi_add_host(instance, NULL);
268 if (error)
269 goto fail_host;
270
271 platform_set_drvdata(pdev, instance);
272
273 scsi_scan_host(instance);
274 return 0;
275
276fail_host:
277 free_irq(IRQ_AMIGA_PORTS, instance);
278fail_irq:
279 scsi_host_put(instance);
280fail_alloc:
281 release_mem_region(res->start, resource_size(res));
282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200285static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
286{
287 struct Scsi_Host *instance = platform_get_drvdata(pdev);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200288 struct a3000_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200289 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
290
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200291 hdata->regs->CNTR = 0;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200292 scsi_remove_host(instance);
293 free_irq(IRQ_AMIGA_PORTS, instance);
294 scsi_host_put(instance);
295 release_mem_region(res->start, resource_size(res));
296 return 0;
297}
298
299static struct platform_driver amiga_a3000_scsi_driver = {
300 .remove = __exit_p(amiga_a3000_scsi_remove),
301 .driver = {
302 .name = "amiga-a3000-scsi",
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200303 },
304};
305
Jingoo Hana915b842013-05-02 15:30:31 +0900306module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200307
308MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309MODULE_LICENSE("GPL");
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200310MODULE_ALIAS("platform:amiga-a3000-scsi");