Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * Buddha, Catweasel and X-Surf PATA controller driver
0005  *
0006  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
0007  *      http://www.samsung.com
0008  *
0009  * Based on buddha.c:
0010  *
0011  *  Copyright (C) 1997, 2001 by Geert Uytterhoeven and others
0012  */
0013 
0014 #include <linux/ata.h>
0015 #include <linux/blkdev.h>
0016 #include <linux/delay.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/kernel.h>
0019 #include <linux/libata.h>
0020 #include <linux/mm.h>
0021 #include <linux/mod_devicetable.h>
0022 #include <linux/module.h>
0023 #include <linux/types.h>
0024 #include <linux/zorro.h>
0025 #include <scsi/scsi_cmnd.h>
0026 #include <scsi/scsi_host.h>
0027 
0028 #include <asm/amigahw.h>
0029 #include <asm/amigaints.h>
0030 #include <asm/ide.h>
0031 #include <asm/setup.h>
0032 
0033 #define DRV_NAME "pata_buddha"
0034 #define DRV_VERSION "0.1.1"
0035 
0036 #define BUDDHA_BASE1    0x800
0037 #define BUDDHA_BASE2    0xa00
0038 #define BUDDHA_BASE3    0xc00
0039 #define XSURF_BASE1 0xb000 /* 2.5" interface */
0040 #define XSURF_BASE2 0xd000 /* 3.5" interface */
0041 #define BUDDHA_CONTROL  0x11a
0042 #define BUDDHA_IRQ  0xf00
0043 #define XSURF_IRQ   0x7e
0044 #define BUDDHA_IRQ_MR   0xfc0   /* master interrupt enable */
0045 
0046 enum {
0047     BOARD_BUDDHA = 0,
0048     BOARD_CATWEASEL,
0049     BOARD_XSURF
0050 };
0051 
0052 static unsigned int buddha_bases[3] = {
0053     BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
0054 };
0055 
0056 static unsigned int xsurf_bases[2] = {
0057     XSURF_BASE1, XSURF_BASE2
0058 };
0059 
0060 static struct scsi_host_template pata_buddha_sht = {
0061     ATA_PIO_SHT(DRV_NAME),
0062 };
0063 
0064 /* FIXME: is this needed? */
0065 static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
0066                      unsigned char *buf,
0067                      unsigned int buflen, int rw)
0068 {
0069     struct ata_device *dev = qc->dev;
0070     struct ata_port *ap = dev->link->ap;
0071     void __iomem *data_addr = ap->ioaddr.data_addr;
0072     unsigned int words = buflen >> 1;
0073 
0074     /* Transfer multiple of 2 bytes */
0075     if (rw == READ)
0076         raw_insw((u16 *)data_addr, (u16 *)buf, words);
0077     else
0078         raw_outsw((u16 *)data_addr, (u16 *)buf, words);
0079 
0080     /* Transfer trailing byte, if any. */
0081     if (unlikely(buflen & 0x01)) {
0082         unsigned char pad[2] = { };
0083 
0084         /* Point buf to the tail of buffer */
0085         buf += buflen - 1;
0086 
0087         if (rw == READ) {
0088             raw_insw((u16 *)data_addr, (u16 *)pad, 1);
0089             *buf = pad[0];
0090         } else {
0091             pad[0] = *buf;
0092             raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
0093         }
0094         words++;
0095     }
0096 
0097     return words << 1;
0098 }
0099 
0100 /*
0101  * Provide our own set_mode() as we don't want to change anything that has
0102  * already been configured..
0103  */
0104 static int pata_buddha_set_mode(struct ata_link *link,
0105                 struct ata_device **unused)
0106 {
0107     struct ata_device *dev;
0108 
0109     ata_for_each_dev(dev, link, ENABLED) {
0110         /* We don't really care */
0111         dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
0112         dev->xfer_shift = ATA_SHIFT_PIO;
0113         dev->flags |= ATA_DFLAG_PIO;
0114         ata_dev_info(dev, "configured for PIO\n");
0115     }
0116     return 0;
0117 }
0118 
0119 static bool pata_buddha_irq_check(struct ata_port *ap)
0120 {
0121     u8 ch;
0122 
0123     ch = z_readb((unsigned long)ap->private_data);
0124 
0125     return !!(ch & 0x80);
0126 }
0127 
0128 static void pata_xsurf_irq_clear(struct ata_port *ap)
0129 {
0130     z_writeb(0, (unsigned long)ap->private_data);
0131 }
0132 
0133 static struct ata_port_operations pata_buddha_ops = {
0134     .inherits   = &ata_sff_port_ops,
0135     .sff_data_xfer  = pata_buddha_data_xfer,
0136     .sff_irq_check  = pata_buddha_irq_check,
0137     .cable_detect   = ata_cable_unknown,
0138     .set_mode   = pata_buddha_set_mode,
0139 };
0140 
0141 static struct ata_port_operations pata_xsurf_ops = {
0142     .inherits   = &ata_sff_port_ops,
0143     .sff_data_xfer  = pata_buddha_data_xfer,
0144     .sff_irq_check  = pata_buddha_irq_check,
0145     .sff_irq_clear  = pata_xsurf_irq_clear,
0146     .cable_detect   = ata_cable_unknown,
0147     .set_mode   = pata_buddha_set_mode,
0148 };
0149 
0150 static int pata_buddha_probe(struct zorro_dev *z,
0151                  const struct zorro_device_id *ent)
0152 {
0153     static const char * const board_name[] = {
0154         "Buddha", "Catweasel", "X-Surf"
0155     };
0156     struct ata_host *host;
0157     void __iomem *buddha_board;
0158     unsigned long board;
0159     unsigned int type = ent->driver_data;
0160     unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
0161     void *old_drvdata;
0162     int i;
0163 
0164     dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
0165 
0166     board = z->resource.start;
0167 
0168     if (type != BOARD_XSURF) {
0169         if (!devm_request_mem_region(&z->dev,
0170                          board + BUDDHA_BASE1,
0171                          0x800, DRV_NAME))
0172             return -ENXIO;
0173     } else {
0174         if (!devm_request_mem_region(&z->dev,
0175                          board + XSURF_BASE1,
0176                          0x1000, DRV_NAME))
0177             return -ENXIO;
0178         if (!devm_request_mem_region(&z->dev,
0179                          board + XSURF_BASE2,
0180                          0x1000, DRV_NAME)) {
0181         }
0182     }
0183 
0184     /* Workaround for X-Surf: Save drvdata in case zorro8390 has set it */
0185     if (type == BOARD_XSURF)
0186         old_drvdata = dev_get_drvdata(&z->dev);
0187 
0188     /* allocate host */
0189     host = ata_host_alloc(&z->dev, nr_ports);
0190     if (type == BOARD_XSURF)
0191         dev_set_drvdata(&z->dev, old_drvdata);
0192     if (!host)
0193         return -ENXIO;
0194 
0195     buddha_board = ZTWO_VADDR(board);
0196 
0197     /* enable the board IRQ on Buddha/Catweasel */
0198     if (type != BOARD_XSURF)
0199         z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
0200 
0201     for (i = 0; i < nr_ports; i++) {
0202         struct ata_port *ap = host->ports[i];
0203         void __iomem *base, *irqport;
0204         unsigned long ctl = 0;
0205 
0206         if (type != BOARD_XSURF) {
0207             ap->ops = &pata_buddha_ops;
0208             base = buddha_board + buddha_bases[i];
0209             ctl = BUDDHA_CONTROL;
0210             irqport = buddha_board + BUDDHA_IRQ + i * 0x40;
0211         } else {
0212             ap->ops = &pata_xsurf_ops;
0213             base = buddha_board + xsurf_bases[i];
0214             /* X-Surf has no CS1* (Control/AltStat) */
0215             irqport = buddha_board + XSURF_IRQ;
0216         }
0217 
0218         ap->pio_mask = ATA_PIO4;
0219         ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
0220 
0221         ap->ioaddr.data_addr        = base;
0222         ap->ioaddr.error_addr       = base + 2 + 1 * 4;
0223         ap->ioaddr.feature_addr     = base + 2 + 1 * 4;
0224         ap->ioaddr.nsect_addr       = base + 2 + 2 * 4;
0225         ap->ioaddr.lbal_addr        = base + 2 + 3 * 4;
0226         ap->ioaddr.lbam_addr        = base + 2 + 4 * 4;
0227         ap->ioaddr.lbah_addr        = base + 2 + 5 * 4;
0228         ap->ioaddr.device_addr      = base + 2 + 6 * 4;
0229         ap->ioaddr.status_addr      = base + 2 + 7 * 4;
0230         ap->ioaddr.command_addr     = base + 2 + 7 * 4;
0231 
0232         if (ctl) {
0233             ap->ioaddr.altstatus_addr = base + ctl;
0234             ap->ioaddr.ctl_addr   = base + ctl;
0235         }
0236 
0237         ap->private_data = (void *)irqport;
0238 
0239         ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
0240                   ctl ? board + buddha_bases[i] + ctl : 0);
0241     }
0242 
0243     ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
0244               IRQF_SHARED, &pata_buddha_sht);
0245 
0246     return 0;
0247 }
0248 
0249 static void pata_buddha_remove(struct zorro_dev *z)
0250 {
0251     struct ata_host *host = dev_get_drvdata(&z->dev);
0252 
0253     ata_host_detach(host);
0254 }
0255 
0256 static const struct zorro_device_id pata_buddha_zorro_tbl[] = {
0257     { ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA, BOARD_BUDDHA},
0258     { ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL, BOARD_CATWEASEL},
0259     { 0 }
0260 };
0261 MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
0262 
0263 static struct zorro_driver pata_buddha_driver = {
0264     .name           = "pata_buddha",
0265     .id_table       = pata_buddha_zorro_tbl,
0266     .probe          = pata_buddha_probe,
0267     .remove         = pata_buddha_remove,
0268 };
0269 
0270 /*
0271  * We cannot have a modalias for X-Surf boards, as it competes with the
0272  * zorro8390 network driver. As a stopgap measure until we have proper
0273  * MFD support for this board, we manually attach to it late after Zorro
0274  * has enumerated its boards.
0275  */
0276 static int __init pata_buddha_late_init(void)
0277 {
0278     struct zorro_dev *z = NULL;
0279 
0280     /* Auto-bind to regular boards */
0281     zorro_register_driver(&pata_buddha_driver);
0282 
0283     /* Manually bind to all X-Surf boards */
0284     while ((z = zorro_find_device(ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, z))) {
0285         static struct zorro_device_id xsurf_ent = {
0286             ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, BOARD_XSURF
0287         };
0288 
0289         pata_buddha_probe(z, &xsurf_ent);
0290     }
0291 
0292     return 0;
0293 }
0294 late_initcall(pata_buddha_late_init);
0295 
0296 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
0297 MODULE_DESCRIPTION("low-level driver for Buddha/Catweasel/X-Surf PATA");
0298 MODULE_LICENSE("GPL v2");
0299 MODULE_VERSION(DRV_VERSION);