Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * A low-level PATA driver to handle a Compact Flash connected on the |
| 4 | * Mikrotik's RouterBoard 532 board. |
| 5 | * |
| 6 | * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org> |
| 7 | * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org> |
| 8 | * |
| 9 | * This file was based on: drivers/ata/pata_ixp4xx_cf.c |
| 10 | * Copyright (C) 2006-07 Tower Technologies |
| 11 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 12 | * |
| 13 | * Also was based on the driver for Linux 2.4.xx published by Mikrotik for |
| 14 | * their RouterBoard 1xx and 5xx series devices. The original Mikrotik code |
| 15 | * seems not to have a license. |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 16 | */ |
| 17 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 26 | #include <linux/gpio/consumer.h> |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 27 | |
| 28 | #include <linux/libata.h> |
| 29 | #include <scsi/scsi_host.h> |
| 30 | |
Gabor Juhos | 0183617 | 2016-02-17 12:58:20 +0100 | [diff] [blame] | 31 | #include <asm/mach-rc32434/rb.h> |
| 32 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 33 | #define DRV_NAME "pata-rb532-cf" |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 34 | #define DRV_VERSION "0.1.0" |
| 35 | #define DRV_DESC "PATA driver for RouterBOARD 532 Compact Flash" |
| 36 | |
| 37 | #define RB500_CF_MAXPORTS 1 |
| 38 | #define RB500_CF_IO_DELAY 400 |
| 39 | |
Phil Sutter | 9f14786 | 2008-11-28 20:48:26 +0100 | [diff] [blame] | 40 | #define RB500_CF_REG_BASE 0x0800 |
| 41 | #define RB500_CF_REG_ERR 0x080D |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 42 | #define RB500_CF_REG_CTRL 0x080E |
Phil Sutter | 9f14786 | 2008-11-28 20:48:26 +0100 | [diff] [blame] | 43 | /* 32bit buffered data register offset */ |
| 44 | #define RB500_CF_REG_DBUF32 0x0C00 |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 45 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 46 | struct rb532_cf_info { |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 47 | void __iomem *iobase; |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 48 | struct gpio_desc *gpio_line; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 49 | unsigned int irq; |
| 50 | }; |
| 51 | |
| 52 | /* ------------------------------------------------------------------------ */ |
| 53 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 54 | static irqreturn_t rb532_pata_irq_handler(int irq, void *dev_instance) |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 55 | { |
| 56 | struct ata_host *ah = dev_instance; |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 57 | struct rb532_cf_info *info = ah->private_data; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 58 | |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 59 | if (gpiod_get_value(info->gpio_line)) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 60 | irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW); |
Phil Sutter | 6be976e | 2009-01-27 14:35:53 +0100 | [diff] [blame] | 61 | ata_sff_interrupt(info->irq, dev_instance); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 62 | } else { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 63 | irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return IRQ_HANDLED; |
| 67 | } |
| 68 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 69 | static struct ata_port_operations rb532_pata_port_ops = { |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 70 | .inherits = &ata_sff_port_ops, |
Phil Sutter | 180bd14 | 2009-01-27 14:35:52 +0100 | [diff] [blame] | 71 | .sff_data_xfer = ata_sff_data_xfer32, |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /* ------------------------------------------------------------------------ */ |
| 75 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 76 | static struct scsi_host_template rb532_pata_sht = { |
Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 77 | ATA_PIO_SHT(DRV_NAME), |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* ------------------------------------------------------------------------ */ |
| 81 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 82 | static void rb532_pata_setup_ports(struct ata_host *ah) |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 83 | { |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 84 | struct rb532_cf_info *info = ah->private_data; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 85 | struct ata_port *ap; |
| 86 | |
| 87 | ap = ah->ports[0]; |
| 88 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 89 | ap->ops = &rb532_pata_port_ops; |
Erik Inge Bolsø | 14bdef9 | 2009-03-14 21:38:24 +0100 | [diff] [blame] | 90 | ap->pio_mask = ATA_PIO4; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 91 | |
Phil Sutter | 9f14786 | 2008-11-28 20:48:26 +0100 | [diff] [blame] | 92 | ap->ioaddr.cmd_addr = info->iobase + RB500_CF_REG_BASE; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 93 | ap->ioaddr.ctl_addr = info->iobase + RB500_CF_REG_CTRL; |
| 94 | ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL; |
| 95 | |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 96 | ata_sff_std_ports(&ap->ioaddr); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 97 | |
Phil Sutter | 9f14786 | 2008-11-28 20:48:26 +0100 | [diff] [blame] | 98 | ap->ioaddr.data_addr = info->iobase + RB500_CF_REG_DBUF32; |
| 99 | ap->ioaddr.error_addr = info->iobase + RB500_CF_REG_ERR; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 102 | static int rb532_pata_driver_probe(struct platform_device *pdev) |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 103 | { |
Florian Fainelli | 9223d01 | 2009-03-13 15:41:43 +0100 | [diff] [blame] | 104 | int irq; |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 105 | struct gpio_desc *gpiod; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 106 | struct resource *res; |
| 107 | struct ata_host *ah; |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 108 | struct rb532_cf_info *info; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 112 | if (!res) { |
| 113 | dev_err(&pdev->dev, "no IOMEM resource found\n"); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | irq = platform_get_irq(pdev, 0); |
gushengxian | 1af11d0 | 2021-06-22 04:55:07 -0700 | [diff] [blame] | 118 | if (irq < 0) |
Sergey Shtylyov | 2d3a62f | 2021-03-15 14:46:53 +0300 | [diff] [blame] | 119 | return irq; |
Sergey Shtylyov | 2d3a62f | 2021-03-15 14:46:53 +0300 | [diff] [blame] | 120 | if (!irq) |
| 121 | return -EINVAL; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 122 | |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 123 | gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN); |
| 124 | if (IS_ERR(gpiod)) { |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 125 | dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq); |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 126 | return PTR_ERR(gpiod); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 127 | } |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 128 | gpiod_set_consumer_name(gpiod, DRV_NAME); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 129 | |
| 130 | /* allocate host */ |
| 131 | ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS); |
| 132 | if (!ah) |
| 133 | return -ENOMEM; |
| 134 | |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 135 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 136 | if (!info) |
| 137 | return -ENOMEM; |
| 138 | |
| 139 | ah->private_data = info; |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 140 | info->gpio_line = gpiod; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 141 | info->irq = irq; |
| 142 | |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 143 | info->iobase = devm_ioremap(&pdev->dev, res->start, |
Julia Lawall | 041b5ea | 2009-08-06 16:05:08 -0700 | [diff] [blame] | 144 | resource_size(res)); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 145 | if (!info->iobase) |
| 146 | return -ENOMEM; |
| 147 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 148 | rb532_pata_setup_ports(ah); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 149 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 150 | ret = ata_host_activate(ah, irq, rb532_pata_irq_handler, |
| 151 | IRQF_TRIGGER_LOW, &rb532_pata_sht); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 152 | if (ret) |
Linus Walleij | cd56f35 | 2018-11-20 08:47:32 +0100 | [diff] [blame] | 153 | return ret; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 154 | |
| 155 | return 0; |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 158 | static int rb532_pata_driver_remove(struct platform_device *pdev) |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 159 | { |
| 160 | struct ata_host *ah = platform_get_drvdata(pdev); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 161 | |
| 162 | ata_host_detach(ah); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Ralf Baechle | 3dd654b | 2008-04-28 12:41:36 +0100 | [diff] [blame] | 167 | static struct platform_driver rb532_pata_platform_driver = { |
| 168 | .probe = rb532_pata_driver_probe, |
Greg Kroah-Hartman | 0ec2491 | 2012-12-21 13:19:58 -0800 | [diff] [blame] | 169 | .remove = rb532_pata_driver_remove, |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 170 | .driver = { |
| 171 | .name = DRV_NAME, |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 172 | }, |
| 173 | }; |
| 174 | |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 175 | #define DRV_INFO DRV_DESC " version " DRV_VERSION |
| 176 | |
Axel Lin | 99c8ea3 | 2011-11-27 14:44:26 +0800 | [diff] [blame] | 177 | module_platform_driver(rb532_pata_platform_driver); |
Florian Fainelli | 3db691d | 2008-03-06 12:25:21 +0100 | [diff] [blame] | 178 | |
| 179 | MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>"); |
| 180 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 181 | MODULE_DESCRIPTION(DRV_DESC); |
| 182 | MODULE_VERSION(DRV_VERSION); |
| 183 | MODULE_LICENSE("GPL"); |
Axel Lin | 99c8ea3 | 2011-11-27 14:44:26 +0800 | [diff] [blame] | 184 | MODULE_ALIAS("platform:" DRV_NAME); |