drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / a3000.c
CommitLineData
1da177e4
LT
1#include <linux/types.h>
2#include <linux/mm.h>
1da177e4
LT
3#include <linux/ioport.h>
4#include <linux/init.h>
c2a24a4c 5#include <linux/slab.h>
1da177e4
LT
6#include <linux/spinlock.h>
7#include <linux/interrupt.h>
c2a24a4c 8#include <linux/platform_device.h>
acf3368f 9#include <linux/module.h>
1da177e4 10
1da177e4
LT
11#include <asm/page.h>
12#include <asm/pgtable.h>
13#include <asm/amigaints.h>
14#include <asm/amigahw.h>
1da177e4
LT
15
16#include "scsi.h"
1da177e4
LT
17#include "wd33c93.h"
18#include "a3000.h"
19
9387edbe 20
2b21d5e4
GU
21struct a3000_hostdata {
22 struct WD33C93_hostdata wh;
23 struct a3000_scsiregs *regs;
24};
25
a8169e60 26static irqreturn_t a3000_intr(int irq, void *data)
1da177e4 27{
a8169e60 28 struct Scsi_Host *instance = data;
2b21d5e4
GU
29 struct a3000_hostdata *hdata = shost_priv(instance);
30 unsigned int status = hdata->regs->ISTR;
1da177e4 31 unsigned long flags;
1da177e4
LT
32
33 if (!(status & ISTR_INT_P))
34 return IRQ_NONE;
21351013 35 if (status & ISTR_INTS) {
a8169e60
GU
36 spin_lock_irqsave(instance->host_lock, flags);
37 wd33c93_intr(instance);
38 spin_unlock_irqrestore(instance->host_lock, flags);
1da177e4
LT
39 return IRQ_HANDLED;
40 }
c2a24a4c 41 pr_warning("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
1da177e4
LT
42 return IRQ_NONE;
43}
44
65396410 45static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
1da177e4 46{
a8169e60 47 struct Scsi_Host *instance = cmd->device->host;
2b21d5e4
GU
48 struct a3000_hostdata *hdata = shost_priv(instance);
49 struct WD33C93_hostdata *wh = &hdata->wh;
50 struct a3000_scsiregs *regs = hdata->regs;
21351013
GU
51 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
52 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
53
54 /*
55 * if the physical address has the wrong alignment, or if
56 * physical address is bad, or if it is a write and at the
57 * end of a physical memory chunk, then allocate a bounce
58 * buffer
59 */
60 if (addr & A3000_XFER_MASK) {
2b21d5e4
GU
61 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
62 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
63 GFP_KERNEL);
21351013
GU
64
65 /* can't allocate memory; use PIO */
2b21d5e4
GU
66 if (!wh->dma_bounce_buffer) {
67 wh->dma_bounce_len = 0;
21351013
GU
68 return 1;
69 }
70
71 if (!dir_in) {
72 /* copy to bounce buffer for a write */
2b21d5e4 73 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
afdbbc16 74 cmd->SCp.this_residual);
21351013
GU
75 }
76
2b21d5e4 77 addr = virt_to_bus(wh->dma_bounce_buffer);
1da177e4
LT
78 }
79
21351013
GU
80 /* setup dma direction */
81 if (!dir_in)
82 cntr |= CNTR_DDIR;
1da177e4 83
21351013 84 /* remember direction */
2b21d5e4 85 wh->dma_dir = dir_in;
1da177e4 86
d753722e 87 regs->CNTR = cntr;
1da177e4 88
21351013 89 /* setup DMA *physical* address */
d753722e 90 regs->ACR = addr;
1da177e4 91
21351013
GU
92 if (dir_in) {
93 /* invalidate any cache */
94 cache_clear(addr, cmd->SCp.this_residual);
95 } else {
96 /* push any dirty cache */
97 cache_push(addr, cmd->SCp.this_residual);
98 }
1da177e4 99
21351013
GU
100 /* start DMA */
101 mb(); /* make sure setup is completed */
d753722e 102 regs->ST_DMA = 1;
21351013 103 mb(); /* make sure DMA has started before next IO */
1da177e4 104
21351013
GU
105 /* return success */
106 return 0;
1da177e4
LT
107}
108
65396410
HK
109static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
110 int status)
1da177e4 111{
2b21d5e4
GU
112 struct a3000_hostdata *hdata = shost_priv(instance);
113 struct WD33C93_hostdata *wh = &hdata->wh;
114 struct a3000_scsiregs *regs = hdata->regs;
afdbbc16 115
21351013
GU
116 /* disable SCSI interrupts */
117 unsigned short cntr = CNTR_PDMD;
118
2b21d5e4 119 if (!wh->dma_dir)
21351013
GU
120 cntr |= CNTR_DDIR;
121
d753722e 122 regs->CNTR = cntr;
21351013
GU
123 mb(); /* make sure CNTR is updated before next IO */
124
125 /* flush if we were reading */
2b21d5e4 126 if (wh->dma_dir) {
d753722e 127 regs->FLUSH = 1;
21351013 128 mb(); /* don't allow prefetch */
d753722e 129 while (!(regs->ISTR & ISTR_FE_FLG))
21351013
GU
130 barrier();
131 mb(); /* no IO until FLUSH is done */
132 }
133
134 /* clear a possible interrupt */
135 /* I think that this CINT is only necessary if you are
136 * using the terminal count features. HM 7 Mar 1994
137 */
d753722e 138 regs->CINT = 1;
21351013
GU
139
140 /* stop DMA */
d753722e 141 regs->SP_DMA = 1;
21351013
GU
142 mb(); /* make sure DMA is stopped before next IO */
143
144 /* restore the CONTROL bits (minus the direction flag) */
d753722e 145 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
21351013
GU
146 mb(); /* make sure CNTR is updated before next IO */
147
148 /* copy from a bounce buffer, if necessary */
2b21d5e4 149 if (status && wh->dma_bounce_buffer) {
21351013 150 if (SCpnt) {
2b21d5e4
GU
151 if (wh->dma_dir && SCpnt)
152 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
21351013 153 SCpnt->SCp.this_residual);
2b21d5e4
GU
154 kfree(wh->dma_bounce_buffer);
155 wh->dma_bounce_buffer = NULL;
156 wh->dma_bounce_len = 0;
21351013 157 } else {
2b21d5e4
GU
158 kfree(wh->dma_bounce_buffer);
159 wh->dma_bounce_buffer = NULL;
160 wh->dma_bounce_len = 0;
21351013 161 }
1da177e4 162 }
1da177e4
LT
163}
164
c2a24a4c
GU
165static int a3000_bus_reset(struct scsi_cmnd *cmd)
166{
167 struct Scsi_Host *instance = cmd->device->host;
168
169 /* FIXME perform bus-specific reset */
170
171 /* FIXME 2: kill this entire function, which should
172 cause mid-layer to call wd33c93_host_reset anyway? */
173
174 spin_lock_irq(instance->host_lock);
175 wd33c93_host_reset(cmd);
176 spin_unlock_irq(instance->host_lock);
177
178 return SUCCESS;
179}
180
181static struct scsi_host_template amiga_a3000_scsi_template = {
182 .module = THIS_MODULE,
183 .name = "Amiga 3000 built-in SCSI",
408bb25b
AV
184 .show_info = wd33c93_show_info,
185 .write_info = wd33c93_write_info,
c2a24a4c
GU
186 .proc_name = "A3000",
187 .queuecommand = wd33c93_queuecommand,
188 .eh_abort_handler = wd33c93_abort,
189 .eh_bus_reset_handler = a3000_bus_reset,
190 .eh_host_reset_handler = wd33c93_host_reset,
191 .can_queue = CAN_QUEUE,
192 .this_id = 7,
193 .sg_tablesize = SG_ALL,
194 .cmd_per_lun = CMD_PER_LUN,
195 .use_clustering = ENABLE_CLUSTERING
196};
197
198static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
1da177e4 199{
c2a24a4c 200 struct resource *res;
a8169e60 201 struct Scsi_Host *instance;
c2a24a4c 202 int error;
c57c1cab 203 struct a3000_scsiregs *regs;
c2a24a4c 204 wd33c93_regs wdregs;
2b21d5e4 205 struct a3000_hostdata *hdata;
21351013 206
c2a24a4c
GU
207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 if (!res)
209 return -ENODEV;
21351013 210
c2a24a4c
GU
211 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
212 return -EBUSY;
21351013 213
c2a24a4c 214 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
2b21d5e4 215 sizeof(struct a3000_hostdata));
c2a24a4c
GU
216 if (!instance) {
217 error = -ENOMEM;
218 goto fail_alloc;
219 }
21351013 220
a8169e60 221 instance->irq = IRQ_AMIGA_PORTS;
c2a24a4c 222
2b21d5e4 223 regs = (struct a3000_scsiregs *)ZTWO_VADDR(res->start);
d753722e 224 regs->DAWR = DAWR_A3000;
c2a24a4c 225
d753722e
GU
226 wdregs.SASR = &regs->SASR;
227 wdregs.SCMD = &regs->SCMD;
c2a24a4c 228
a8169e60 229 hdata = shost_priv(instance);
2b21d5e4
GU
230 hdata->wh.no_sync = 0xff;
231 hdata->wh.fast = 0;
232 hdata->wh.dma_mode = CTRL_DMA;
233 hdata->regs = regs;
c2a24a4c 234
a8169e60 235 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
c2a24a4c
GU
236 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
237 "A3000 SCSI", instance);
238 if (error)
21351013 239 goto fail_irq;
c2a24a4c 240
d753722e 241 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
21351013 242
c2a24a4c
GU
243 error = scsi_add_host(instance, NULL);
244 if (error)
245 goto fail_host;
1da177e4 246
c2a24a4c
GU
247 platform_set_drvdata(pdev, instance);
248
249 scsi_scan_host(instance);
21351013 250 return 0;
c2a24a4c
GU
251
252fail_host:
253 free_irq(IRQ_AMIGA_PORTS, instance);
254fail_irq:
255 scsi_host_put(instance);
256fail_alloc:
257 release_mem_region(res->start, resource_size(res));
258 return error;
1da177e4
LT
259}
260
c2a24a4c 261static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
1da177e4 262{
c2a24a4c 263 struct Scsi_Host *instance = platform_get_drvdata(pdev);
2b21d5e4 264 struct a3000_hostdata *hdata = shost_priv(instance);
c2a24a4c 265 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
68b3aa7c 266
2b21d5e4 267 hdata->regs->CNTR = 0;
c2a24a4c
GU
268 scsi_remove_host(instance);
269 free_irq(IRQ_AMIGA_PORTS, instance);
270 scsi_host_put(instance);
271 release_mem_region(res->start, resource_size(res));
272 return 0;
1da177e4
LT
273}
274
c2a24a4c
GU
275static struct platform_driver amiga_a3000_scsi_driver = {
276 .remove = __exit_p(amiga_a3000_scsi_remove),
277 .driver = {
278 .name = "amiga-a3000-scsi",
279 .owner = THIS_MODULE,
280 },
1da177e4
LT
281};
282
c2a24a4c 283static int __init amiga_a3000_scsi_init(void)
1da177e4 284{
c2a24a4c
GU
285 return platform_driver_probe(&amiga_a3000_scsi_driver,
286 amiga_a3000_scsi_probe);
287}
288module_init(amiga_a3000_scsi_init);
d753722e 289
c2a24a4c
GU
290static void __exit amiga_a3000_scsi_exit(void)
291{
292 platform_driver_unregister(&amiga_a3000_scsi_driver);
1da177e4 293}
c2a24a4c 294module_exit(amiga_a3000_scsi_exit);
1da177e4 295
c2a24a4c 296MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
1da177e4 297MODULE_LICENSE("GPL");
c2a24a4c 298MODULE_ALIAS("platform:amiga-a3000-scsi");