blob: 78f33d57c3e8d9849a1828f3bb3fe0ee51361370 [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/*
3 * Oak Generic NCR5380 driver
4 *
5 * Copyright 1995-2002, Russell King
6 */
7
8#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/blkdev.h>
11#include <linux/init.h>
12
13#include <asm/ecard.h>
14#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <scsi/scsi_host.h>
17
Russell King8b801ea2007-07-20 10:38:54 +010018#define priv(host) ((struct NCR5380_hostdata *)(host)->hostdata)
Russell King8b801ea2007-07-20 10:38:54 +010019
Finn Thain61e1ce52016-10-10 00:46:53 -040020#define NCR5380_read(reg) readb(hostdata->io + ((reg) << 2))
21#define NCR5380_write(reg, value) writeb(value, hostdata->io + ((reg) << 2))
Finn Thain54d8fe42016-01-03 16:05:06 +110022
Finn Thain4a98f892016-10-10 00:46:53 -040023#define NCR5380_dma_xfer_len NCR5380_dma_xfer_none
Finn Thain6c4b88c2016-03-23 21:10:17 +110024#define NCR5380_dma_recv_setup oakscsi_pread
25#define NCR5380_dma_send_setup oakscsi_pwrite
Finn Thain4a98f892016-10-10 00:46:53 -040026#define NCR5380_dma_residual NCR5380_dma_residual_none
Finn Thainff3d4572016-01-03 16:05:25 +110027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define NCR5380_queue_command oakscsi_queue_command
Finn Thain8c325132014-11-12 16:11:58 +110029#define NCR5380_info oakscsi_info
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Finn Thain820682b2016-10-10 00:46:53 -040031#define NCR5380_implementation_fields /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "../NCR5380.h"
34
35#undef START_DMA_INITIATOR_RECEIVE_REG
Russell King8b801ea2007-07-20 10:38:54 +010036#define START_DMA_INITIATOR_RECEIVE_REG (128 + 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Russell King8b801ea2007-07-20 10:38:54 +010038#define STAT ((128 + 16) << 2)
39#define DATA ((128 + 8) << 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Finn Thain4a98f892016-10-10 00:46:53 -040041static inline int oakscsi_pwrite(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +110042 unsigned char *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Finn Thain4a98f892016-10-10 00:46:53 -040044 u8 __iomem *base = hostdata->io;
Russell King8b801ea2007-07-20 10:38:54 +010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046printk("writing %p len %d\n",addr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 while(1)
49 {
50 int status;
Russell King8b801ea2007-07-20 10:38:54 +010051 while (((status = readw(base + STAT)) & 0x100)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Finn Thain438af512016-03-23 21:10:18 +110053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Finn Thain4a98f892016-10-10 00:46:53 -040056static inline int oakscsi_pread(struct NCR5380_hostdata *hostdata,
Finn Thain6c4b88c2016-03-23 21:10:17 +110057 unsigned char *addr, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Finn Thain4a98f892016-10-10 00:46:53 -040059 u8 __iomem *base = hostdata->io;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061printk("reading %p len %d\n", addr, len);
62 while(len > 0)
63 {
Russell King8b801ea2007-07-20 10:38:54 +010064 unsigned int status, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned long b;
66
67 timeout = 0x01FFFFFF;
68
Russell King8b801ea2007-07-20 10:38:54 +010069 while (((status = readw(base + STAT)) & 0x100)==0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 {
71 timeout--;
72 if(status & 0x200 || !timeout)
73 {
Russell King8b801ea2007-07-20 10:38:54 +010074 printk("status = %08X\n", status);
Finn Thain438af512016-03-23 21:10:18 +110075 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 }
Russell King8b801ea2007-07-20 10:38:54 +010078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if(len >= 128)
80 {
Russell King8b801ea2007-07-20 10:38:54 +010081 readsw(base + DATA, addr, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 addr += 128;
83 len -= 128;
84 }
85 else
86 {
Russell King8b801ea2007-07-20 10:38:54 +010087 b = (unsigned long) readw(base + DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *addr ++ = b;
89 len -= 1;
90 if(len)
91 *addr ++ = b>>8;
92 len -= 1;
93 }
94 }
95 return 0;
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#undef STAT
Russell King8b801ea2007-07-20 10:38:54 +010099#undef DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include "../NCR5380.c"
102
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100103static struct scsi_host_template oakscsi_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .name = "Oak 16-bit SCSI",
106 .info = oakscsi_info,
107 .queuecommand = oakscsi_queue_command,
108 .eh_abort_handler = NCR5380_abort,
Hannes Reinecke12e5fc62017-08-25 13:57:10 +0200109 .eh_host_reset_handler = NCR5380_host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .can_queue = 16,
111 .this_id = 7,
112 .sg_tablesize = SG_ALL,
113 .cmd_per_lun = 2,
Christoph Hellwig4af14d12018-12-13 16:17:09 +0100114 .dma_boundary = PAGE_SIZE - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_name = "oakscsi",
Finn Thain32b26a12016-01-03 16:05:58 +1100116 .cmd_size = NCR5380_CMD_SIZE,
Finn Thain0a4e3612016-01-03 16:06:07 +1100117 .max_sectors = 128,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800120static int oakscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct Scsi_Host *host;
Jing Xiangfeng713a8462020-09-18 17:07:47 +0800123 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Russell King8b801ea2007-07-20 10:38:54 +0100125 ret = ecard_request_resources(ec);
126 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto out;
128
Russell King8b801ea2007-07-20 10:38:54 +0100129 host = scsi_host_alloc(&oakscsi_template, sizeof(struct NCR5380_hostdata));
130 if (!host) {
131 ret = -ENOMEM;
132 goto release;
133 }
134
Finn Thain820682b2016-10-10 00:46:53 -0400135 priv(host)->io = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
136 ecard_resource_len(ec, ECARD_RES_MEMC));
137 if (!priv(host)->io) {
Russell King8b801ea2007-07-20 10:38:54 +0100138 ret = -ENOMEM;
139 goto unreg;
140 }
141
Finn Thain22f5f102014-11-12 16:11:56 +1100142 host->irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Finn Thain8053b0e2016-03-23 21:10:19 +1100144 ret = NCR5380_init(host, FLAG_DMA_FIXUP | FLAG_LATE_DMA_SETUP);
Finn Thain0ad0eff2016-01-03 16:05:21 +1100145 if (ret)
146 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Finn Thainb6488f92016-01-03 16:05:08 +1100148 NCR5380_maybe_reset_bus(host);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ret = scsi_add_host(host, &ec->dev);
151 if (ret)
Finn Thain0ad0eff2016-01-03 16:05:21 +1100152 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 scsi_scan_host(host);
155 goto out;
156
Finn Thain0ad0eff2016-01-03 16:05:21 +1100157 out_exit:
158 NCR5380_exit(host);
Russell King8b801ea2007-07-20 10:38:54 +0100159 out_unmap:
Finn Thain820682b2016-10-10 00:46:53 -0400160 iounmap(priv(host)->io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unreg:
162 scsi_host_put(host);
Russell King8b801ea2007-07-20 10:38:54 +0100163 release:
164 ecard_release_resources(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 out:
166 return ret;
167}
168
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800169static void oakscsi_remove(struct expansion_card *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct Scsi_Host *host = ecard_get_drvdata(ec);
Finn Thain820682b2016-10-10 00:46:53 -0400172 void __iomem *base = priv(host)->io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 ecard_set_drvdata(ec, NULL);
175 scsi_remove_host(host);
176
177 NCR5380_exit(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 scsi_host_put(host);
Finn Thain820682b2016-10-10 00:46:53 -0400179 iounmap(base);
Russell King8b801ea2007-07-20 10:38:54 +0100180 ecard_release_resources(ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183static const struct ecard_id oakscsi_cids[] = {
184 { MANU_OAK, PROD_OAK_SCSI },
185 { 0xffff, 0xffff }
186};
187
188static struct ecard_driver oakscsi_driver = {
189 .probe = oakscsi_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800190 .remove = oakscsi_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .id_table = oakscsi_cids,
192 .drv = {
193 .name = "oakscsi",
194 },
195};
196
197static int __init oakscsi_init(void)
198{
199 return ecard_register_driver(&oakscsi_driver);
200}
201
202static void __exit oakscsi_exit(void)
203{
204 ecard_remove_driver(&oakscsi_driver);
205}
206
207module_init(oakscsi_init);
208module_exit(oakscsi_exit);
209
210MODULE_AUTHOR("Russell King");
211MODULE_DESCRIPTION("Oak SCSI driver");
212MODULE_LICENSE("GPL");
213