scsi: esp_scsi: use strong typing for the dev field
[linux-2.6-block.git] / drivers / scsi / sun_esp.c
1 /* sun_esp.c: ESP front-end for Sparc SBUS systems.
2  *
3  * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/delay.h>
9 #include <linux/module.h>
10 #include <linux/mm.h>
11 #include <linux/init.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/gfp.h>
16
17 #include <asm/irq.h>
18 #include <asm/io.h>
19 #include <asm/dma.h>
20
21 #include <scsi/scsi_host.h>
22
23 #include "esp_scsi.h"
24
25 #define DRV_MODULE_NAME         "sun_esp"
26 #define PFX DRV_MODULE_NAME     ": "
27 #define DRV_VERSION             "1.100"
28 #define DRV_MODULE_RELDATE      "August 27, 2008"
29
30 #define dma_read32(REG) \
31         sbus_readl(esp->dma_regs + (REG))
32 #define dma_write32(VAL, REG) \
33         sbus_writel((VAL), esp->dma_regs + (REG))
34
35 /* DVMA chip revisions */
36 enum dvma_rev {
37         dvmarev0,
38         dvmaesc1,
39         dvmarev1,
40         dvmarev2,
41         dvmarev3,
42         dvmarevplus,
43         dvmahme
44 };
45
46 static int esp_sbus_setup_dma(struct esp *esp, struct platform_device *dma_of)
47 {
48         esp->dma = dma_of;
49
50         esp->dma_regs = of_ioremap(&dma_of->resource[0], 0,
51                                    resource_size(&dma_of->resource[0]),
52                                    "espdma");
53         if (!esp->dma_regs)
54                 return -ENOMEM;
55
56         switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) {
57         case DMA_VERS0:
58                 esp->dmarev = dvmarev0;
59                 break;
60         case DMA_ESCV1:
61                 esp->dmarev = dvmaesc1;
62                 break;
63         case DMA_VERS1:
64                 esp->dmarev = dvmarev1;
65                 break;
66         case DMA_VERS2:
67                 esp->dmarev = dvmarev2;
68                 break;
69         case DMA_VERHME:
70                 esp->dmarev = dvmahme;
71                 break;
72         case DMA_VERSPLUS:
73                 esp->dmarev = dvmarevplus;
74                 break;
75         }
76
77         return 0;
78
79 }
80
81 static int esp_sbus_map_regs(struct esp *esp, int hme)
82 {
83         struct platform_device *op = to_platform_device(esp->dev);
84         struct resource *res;
85
86         /* On HME, two reg sets exist, first is DVMA,
87          * second is ESP registers.
88          */
89         if (hme)
90                 res = &op->resource[1];
91         else
92                 res = &op->resource[0];
93
94         esp->regs = of_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP");
95         if (!esp->regs)
96                 return -ENOMEM;
97
98         return 0;
99 }
100
101 static int esp_sbus_map_command_block(struct esp *esp)
102 {
103         esp->command_block = dma_alloc_coherent(esp->dev, 16,
104                                                 &esp->command_block_dma,
105                                                 GFP_KERNEL);
106         if (!esp->command_block)
107                 return -ENOMEM;
108         return 0;
109 }
110
111 static int esp_sbus_register_irq(struct esp *esp)
112 {
113         struct Scsi_Host *host = esp->host;
114         struct platform_device *op = to_platform_device(esp->dev);
115
116         host->irq = op->archdata.irqs[0];
117         return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
118 }
119
120 static void esp_get_scsi_id(struct esp *esp, struct platform_device *espdma)
121 {
122         struct platform_device *op = to_platform_device(esp->dev);
123         struct device_node *dp;
124
125         dp = op->dev.of_node;
126         esp->scsi_id = of_getintprop_default(dp, "initiator-id", 0xff);
127         if (esp->scsi_id != 0xff)
128                 goto done;
129
130         esp->scsi_id = of_getintprop_default(dp, "scsi-initiator-id", 0xff);
131         if (esp->scsi_id != 0xff)
132                 goto done;
133
134         esp->scsi_id = of_getintprop_default(espdma->dev.of_node,
135                                              "scsi-initiator-id", 7);
136
137 done:
138         esp->host->this_id = esp->scsi_id;
139         esp->scsi_id_mask = (1 << esp->scsi_id);
140 }
141
142 static void esp_get_differential(struct esp *esp)
143 {
144         struct platform_device *op = to_platform_device(esp->dev);
145         struct device_node *dp;
146
147         dp = op->dev.of_node;
148         if (of_find_property(dp, "differential", NULL))
149                 esp->flags |= ESP_FLAG_DIFFERENTIAL;
150         else
151                 esp->flags &= ~ESP_FLAG_DIFFERENTIAL;
152 }
153
154 static void esp_get_clock_params(struct esp *esp)
155 {
156         struct platform_device *op = to_platform_device(esp->dev);
157         struct device_node *bus_dp, *dp;
158         int fmhz;
159
160         dp = op->dev.of_node;
161         bus_dp = dp->parent;
162
163         fmhz = of_getintprop_default(dp, "clock-frequency", 0);
164         if (fmhz == 0)
165                 fmhz = of_getintprop_default(bus_dp, "clock-frequency", 0);
166
167         esp->cfreq = fmhz;
168 }
169
170 static void esp_get_bursts(struct esp *esp, struct platform_device *dma_of)
171 {
172         struct device_node *dma_dp = dma_of->dev.of_node;
173         struct platform_device *op = to_platform_device(esp->dev);
174         struct device_node *dp;
175         u8 bursts, val;
176
177         dp = op->dev.of_node;
178         bursts = of_getintprop_default(dp, "burst-sizes", 0xff);
179         val = of_getintprop_default(dma_dp, "burst-sizes", 0xff);
180         if (val != 0xff)
181                 bursts &= val;
182
183         val = of_getintprop_default(dma_dp->parent, "burst-sizes", 0xff);
184         if (val != 0xff)
185                 bursts &= val;
186
187         if (bursts == 0xff ||
188             (bursts & DMA_BURST16) == 0 ||
189             (bursts & DMA_BURST32) == 0)
190                 bursts = (DMA_BURST32 - 1);
191
192         esp->bursts = bursts;
193 }
194
195 static void esp_sbus_get_props(struct esp *esp, struct platform_device *espdma)
196 {
197         esp_get_scsi_id(esp, espdma);
198         esp_get_differential(esp);
199         esp_get_clock_params(esp);
200         esp_get_bursts(esp, espdma);
201 }
202
203 static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg)
204 {
205         sbus_writeb(val, esp->regs + (reg * 4UL));
206 }
207
208 static u8 sbus_esp_read8(struct esp *esp, unsigned long reg)
209 {
210         return sbus_readb(esp->regs + (reg * 4UL));
211 }
212
213 static dma_addr_t sbus_esp_map_single(struct esp *esp, void *buf,
214                                       size_t sz, int dir)
215 {
216         return dma_map_single(esp->dev, buf, sz, dir);
217 }
218
219 static int sbus_esp_map_sg(struct esp *esp, struct scatterlist *sg,
220                                   int num_sg, int dir)
221 {
222         return dma_map_sg(esp->dev, sg, num_sg, dir);
223 }
224
225 static void sbus_esp_unmap_single(struct esp *esp, dma_addr_t addr,
226                                   size_t sz, int dir)
227 {
228         dma_unmap_single(esp->dev, addr, sz, dir);
229 }
230
231 static void sbus_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
232                               int num_sg, int dir)
233 {
234         dma_unmap_sg(esp->dev, sg, num_sg, dir);
235 }
236
237 static int sbus_esp_irq_pending(struct esp *esp)
238 {
239         if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
240                 return 1;
241         return 0;
242 }
243
244 static void sbus_esp_reset_dma(struct esp *esp)
245 {
246         int can_do_burst16, can_do_burst32, can_do_burst64;
247         int can_do_sbus64, lim;
248         struct platform_device *op = to_platform_device(esp->dev);
249         u32 val;
250
251         can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
252         can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
253         can_do_burst64 = 0;
254         can_do_sbus64 = 0;
255         if (sbus_can_dma_64bit())
256                 can_do_sbus64 = 1;
257         if (sbus_can_burst64())
258                 can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
259
260         /* Put the DVMA into a known state. */
261         if (esp->dmarev != dvmahme) {
262                 val = dma_read32(DMA_CSR);
263                 dma_write32(val | DMA_RST_SCSI, DMA_CSR);
264                 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
265         }
266         switch (esp->dmarev) {
267         case dvmahme:
268                 dma_write32(DMA_RESET_FAS366, DMA_CSR);
269                 dma_write32(DMA_RST_SCSI, DMA_CSR);
270
271                 esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS |
272                                         DMA_SCSI_DISAB | DMA_INT_ENAB);
273
274                 esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE |
275                                           DMA_BRST_SZ);
276
277                 if (can_do_burst64)
278                         esp->prev_hme_dmacsr |= DMA_BRST64;
279                 else if (can_do_burst32)
280                         esp->prev_hme_dmacsr |= DMA_BRST32;
281
282                 if (can_do_sbus64) {
283                         esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
284                         sbus_set_sbus64(&op->dev, esp->bursts);
285                 }
286
287                 lim = 1000;
288                 while (dma_read32(DMA_CSR) & DMA_PEND_READ) {
289                         if (--lim == 0) {
290                                 printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ "
291                                        "will not clear!\n",
292                                        esp->host->unique_id);
293                                 break;
294                         }
295                         udelay(1);
296                 }
297
298                 dma_write32(0, DMA_CSR);
299                 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
300
301                 dma_write32(0, DMA_ADDR);
302                 break;
303
304         case dvmarev2:
305                 if (esp->rev != ESP100) {
306                         val = dma_read32(DMA_CSR);
307                         dma_write32(val | DMA_3CLKS, DMA_CSR);
308                 }
309                 break;
310
311         case dvmarev3:
312                 val = dma_read32(DMA_CSR);
313                 val &= ~DMA_3CLKS;
314                 val |= DMA_2CLKS;
315                 if (can_do_burst32) {
316                         val &= ~DMA_BRST_SZ;
317                         val |= DMA_BRST32;
318                 }
319                 dma_write32(val, DMA_CSR);
320                 break;
321
322         case dvmaesc1:
323                 val = dma_read32(DMA_CSR);
324                 val |= DMA_ADD_ENABLE;
325                 val &= ~DMA_BCNT_ENAB;
326                 if (!can_do_burst32 && can_do_burst16) {
327                         val |= DMA_ESC_BURST;
328                 } else {
329                         val &= ~(DMA_ESC_BURST);
330                 }
331                 dma_write32(val, DMA_CSR);
332                 break;
333
334         default:
335                 break;
336         }
337
338         /* Enable interrupts.  */
339         val = dma_read32(DMA_CSR);
340         dma_write32(val | DMA_INT_ENAB, DMA_CSR);
341 }
342
343 static void sbus_esp_dma_drain(struct esp *esp)
344 {
345         u32 csr;
346         int lim;
347
348         if (esp->dmarev == dvmahme)
349                 return;
350
351         csr = dma_read32(DMA_CSR);
352         if (!(csr & DMA_FIFO_ISDRAIN))
353                 return;
354
355         if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1)
356                 dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
357
358         lim = 1000;
359         while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
360                 if (--lim == 0) {
361                         printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
362                                esp->host->unique_id);
363                         break;
364                 }
365                 udelay(1);
366         }
367 }
368
369 static void sbus_esp_dma_invalidate(struct esp *esp)
370 {
371         if (esp->dmarev == dvmahme) {
372                 dma_write32(DMA_RST_SCSI, DMA_CSR);
373
374                 esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
375                                          (DMA_PARITY_OFF | DMA_2CLKS |
376                                           DMA_SCSI_DISAB | DMA_INT_ENAB)) &
377                                         ~(DMA_ST_WRITE | DMA_ENABLE));
378
379                 dma_write32(0, DMA_CSR);
380                 dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
381
382                 /* This is necessary to avoid having the SCSI channel
383                  * engine lock up on us.
384                  */
385                 dma_write32(0, DMA_ADDR);
386         } else {
387                 u32 val;
388                 int lim;
389
390                 lim = 1000;
391                 while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
392                         if (--lim == 0) {
393                                 printk(KERN_ALERT PFX "esp%d: DMA will not "
394                                        "invalidate!\n", esp->host->unique_id);
395                                 break;
396                         }
397                         udelay(1);
398                 }
399
400                 val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
401                 val |= DMA_FIFO_INV;
402                 dma_write32(val, DMA_CSR);
403                 val &= ~DMA_FIFO_INV;
404                 dma_write32(val, DMA_CSR);
405         }
406 }
407
408 static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
409                                   u32 dma_count, int write, u8 cmd)
410 {
411         u32 csr;
412
413         BUG_ON(!(cmd & ESP_CMD_DMA));
414
415         sbus_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
416         sbus_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
417         if (esp->rev == FASHME) {
418                 sbus_esp_write8(esp, (esp_count >> 16) & 0xff, FAS_RLO);
419                 sbus_esp_write8(esp, 0, FAS_RHI);
420
421                 scsi_esp_cmd(esp, cmd);
422
423                 csr = esp->prev_hme_dmacsr;
424                 csr |= DMA_SCSI_DISAB | DMA_ENABLE;
425                 if (write)
426                         csr |= DMA_ST_WRITE;
427                 else
428                         csr &= ~DMA_ST_WRITE;
429                 esp->prev_hme_dmacsr = csr;
430
431                 dma_write32(dma_count, DMA_COUNT);
432                 dma_write32(addr, DMA_ADDR);
433                 dma_write32(csr, DMA_CSR);
434         } else {
435                 csr = dma_read32(DMA_CSR);
436                 csr |= DMA_ENABLE;
437                 if (write)
438                         csr |= DMA_ST_WRITE;
439                 else
440                         csr &= ~DMA_ST_WRITE;
441                 dma_write32(csr, DMA_CSR);
442                 if (esp->dmarev == dvmaesc1) {
443                         u32 end = PAGE_ALIGN(addr + dma_count + 16U);
444                         dma_write32(end - addr, DMA_COUNT);
445                 }
446                 dma_write32(addr, DMA_ADDR);
447
448                 scsi_esp_cmd(esp, cmd);
449         }
450
451 }
452
453 static int sbus_esp_dma_error(struct esp *esp)
454 {
455         u32 csr = dma_read32(DMA_CSR);
456
457         if (csr & DMA_HNDL_ERROR)
458                 return 1;
459
460         return 0;
461 }
462
463 static const struct esp_driver_ops sbus_esp_ops = {
464         .esp_write8     =       sbus_esp_write8,
465         .esp_read8      =       sbus_esp_read8,
466         .map_single     =       sbus_esp_map_single,
467         .map_sg         =       sbus_esp_map_sg,
468         .unmap_single   =       sbus_esp_unmap_single,
469         .unmap_sg       =       sbus_esp_unmap_sg,
470         .irq_pending    =       sbus_esp_irq_pending,
471         .reset_dma      =       sbus_esp_reset_dma,
472         .dma_drain      =       sbus_esp_dma_drain,
473         .dma_invalidate =       sbus_esp_dma_invalidate,
474         .send_dma_cmd   =       sbus_esp_send_dma_cmd,
475         .dma_error      =       sbus_esp_dma_error,
476 };
477
478 static int esp_sbus_probe_one(struct platform_device *op,
479                               struct platform_device *espdma, int hme)
480 {
481         struct scsi_host_template *tpnt = &scsi_esp_template;
482         struct Scsi_Host *host;
483         struct esp *esp;
484         int err;
485
486         host = scsi_host_alloc(tpnt, sizeof(struct esp));
487
488         err = -ENOMEM;
489         if (!host)
490                 goto fail;
491
492         host->max_id = (hme ? 16 : 8);
493         esp = shost_priv(host);
494
495         esp->host = host;
496         esp->dev = &op->dev;
497         esp->ops = &sbus_esp_ops;
498
499         if (hme)
500                 esp->flags |= ESP_FLAG_WIDE_CAPABLE;
501
502         err = esp_sbus_setup_dma(esp, espdma);
503         if (err < 0)
504                 goto fail_unlink;
505
506         err = esp_sbus_map_regs(esp, hme);
507         if (err < 0)
508                 goto fail_unlink;
509
510         err = esp_sbus_map_command_block(esp);
511         if (err < 0)
512                 goto fail_unmap_regs;
513
514         err = esp_sbus_register_irq(esp);
515         if (err < 0)
516                 goto fail_unmap_command_block;
517
518         esp_sbus_get_props(esp, espdma);
519
520         /* Before we try to touch the ESP chip, ESC1 dma can
521          * come up with the reset bit set, so make sure that
522          * is clear first.
523          */
524         if (esp->dmarev == dvmaesc1) {
525                 u32 val = dma_read32(DMA_CSR);
526
527                 dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
528         }
529
530         dev_set_drvdata(&op->dev, esp);
531
532         err = scsi_esp_register(esp, &op->dev);
533         if (err)
534                 goto fail_free_irq;
535
536         return 0;
537
538 fail_free_irq:
539         free_irq(host->irq, esp);
540 fail_unmap_command_block:
541         dma_free_coherent(&op->dev, 16,
542                           esp->command_block,
543                           esp->command_block_dma);
544 fail_unmap_regs:
545         of_iounmap(&op->resource[(hme ? 1 : 0)], esp->regs, SBUS_ESP_REG_SIZE);
546 fail_unlink:
547         scsi_host_put(host);
548 fail:
549         return err;
550 }
551
552 static int esp_sbus_probe(struct platform_device *op)
553 {
554         struct device_node *dma_node = NULL;
555         struct device_node *dp = op->dev.of_node;
556         struct platform_device *dma_of = NULL;
557         int hme = 0;
558         int ret;
559
560         if (dp->parent &&
561             (!strcmp(dp->parent->name, "espdma") ||
562              !strcmp(dp->parent->name, "dma")))
563                 dma_node = dp->parent;
564         else if (!strcmp(dp->name, "SUNW,fas")) {
565                 dma_node = op->dev.of_node;
566                 hme = 1;
567         }
568         if (dma_node)
569                 dma_of = of_find_device_by_node(dma_node);
570         if (!dma_of)
571                 return -ENODEV;
572
573         ret = esp_sbus_probe_one(op, dma_of, hme);
574         if (ret)
575                 put_device(&dma_of->dev);
576
577         return ret;
578 }
579
580 static int esp_sbus_remove(struct platform_device *op)
581 {
582         struct esp *esp = dev_get_drvdata(&op->dev);
583         struct platform_device *dma_of = esp->dma;
584         unsigned int irq = esp->host->irq;
585         bool is_hme;
586         u32 val;
587
588         scsi_esp_unregister(esp);
589
590         /* Disable interrupts.  */
591         val = dma_read32(DMA_CSR);
592         dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
593
594         free_irq(irq, esp);
595
596         is_hme = (esp->dmarev == dvmahme);
597
598         dma_free_coherent(&op->dev, 16,
599                           esp->command_block,
600                           esp->command_block_dma);
601         of_iounmap(&op->resource[(is_hme ? 1 : 0)], esp->regs,
602                    SBUS_ESP_REG_SIZE);
603         of_iounmap(&dma_of->resource[0], esp->dma_regs,
604                    resource_size(&dma_of->resource[0]));
605
606         scsi_host_put(esp->host);
607
608         dev_set_drvdata(&op->dev, NULL);
609
610         put_device(&dma_of->dev);
611
612         return 0;
613 }
614
615 static const struct of_device_id esp_match[] = {
616         {
617                 .name = "SUNW,esp",
618         },
619         {
620                 .name = "SUNW,fas",
621         },
622         {
623                 .name = "esp",
624         },
625         {},
626 };
627 MODULE_DEVICE_TABLE(of, esp_match);
628
629 static struct platform_driver esp_sbus_driver = {
630         .driver = {
631                 .name = "esp",
632                 .of_match_table = esp_match,
633         },
634         .probe          = esp_sbus_probe,
635         .remove         = esp_sbus_remove,
636 };
637
638 static int __init sunesp_init(void)
639 {
640         return platform_driver_register(&esp_sbus_driver);
641 }
642
643 static void __exit sunesp_exit(void)
644 {
645         platform_driver_unregister(&esp_sbus_driver);
646 }
647
648 MODULE_DESCRIPTION("Sun ESP SCSI driver");
649 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
650 MODULE_LICENSE("GPL");
651 MODULE_VERSION(DRV_VERSION);
652
653 module_init(sunesp_init);
654 module_exit(sunesp_exit);