[IDE] Add driver for Sibyte Swarm evaluation board
[pandora-kernel.git] / drivers / ide / mips / swarm.c
1 /*
2  * Copyright (C) 2001, 2002, 2003 Broadcom Corporation
3  * Copyright (C) 2004 MontaVista Software Inc.
4  *      Author: Manish Lachwani, mlachwani@mvista.com
5  * Copyright (C) 2004  MIPS Technologies, Inc.  All rights reserved.
6  *      Author: Maciej W. Rozycki <macro@mips.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 /*
24  *  Derived loosely from ide-pmac.c, so:
25  *  Copyright (C) 1998 Paul Mackerras.
26  *  Copyright (C) 1995-1998 Mark Lord
27  */
28
29 /*
30  * Boards with SiByte processors so far have supported IDE devices via
31  * the Generic Bus, PCI bus, and built-in PCMCIA interface.  In all
32  * cases, byte-swapping must be avoided for these devices (whereas
33  * other PCI devices, for example, will require swapping).  Any
34  * SiByte-targetted kernel including IDE support will include this
35  * file.  Probing of a Generic Bus for an IDE device is controlled by
36  * the definition of "SIBYTE_HAVE_IDE", which is provided by
37  * <asm/sibyte/board.h> for Broadcom boards.
38  */
39
40 #include <linux/ide.h>
41 #include <linux/ioport.h>
42 #include <linux/kernel.h>
43 #include <linux/types.h>
44 #include <linux/platform_device.h>
45
46 #include <asm/io.h>
47
48 #include <asm/sibyte/board.h>
49 #include <asm/sibyte/sb1250_genbus.h>
50 #include <asm/sibyte/sb1250_regs.h>
51
52 #define DRV_NAME "ide-swarm"
53
54 static char swarm_ide_string[] = DRV_NAME;
55
56 static struct resource swarm_ide_resource = {
57         .name   = "SWARM GenBus IDE",
58         .flags  = IORESOURCE_MEM,
59 };
60
61 static struct platform_device *swarm_ide_dev;
62
63 /*
64  * swarm_ide_probe - if the board header indicates the existence of
65  * Generic Bus IDE, allocate a HWIF for it.
66  */
67 static int __devinit swarm_ide_probe(struct device *dev)
68 {
69         ide_hwif_t *hwif;
70         u8 __iomem *base;
71         phys_t offset, size;
72         int i;
73
74         if (!SIBYTE_HAVE_IDE)
75                 return -ENODEV;
76
77         /* Find an empty slot.  */
78         for (i = 0; i < MAX_HWIFS; i++)
79                 if (!ide_hwifs[i].io_ports[IDE_DATA_OFFSET])
80                         break;
81         if (i >= MAX_HWIFS) {
82                 printk(KERN_ERR DRV_NAME ": no free slot for interface\n");
83                 return -ENOMEM;
84         }
85
86         hwif = ide_hwifs + i;
87
88         base = ioremap(A_IO_EXT_BASE, 0x800);
89         offset = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_START_ADDR, IDE_CS));
90         size = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_MULT_SIZE, IDE_CS));
91         iounmap(base);
92
93         offset = G_IO_START_ADDR(offset) << S_IO_ADDRBASE;
94         size = (G_IO_MULT_SIZE(size) + 1) << S_IO_REGSIZE;
95         if (offset < A_PHYS_GENBUS || offset >= A_PHYS_GENBUS_END) {
96                 printk(KERN_INFO DRV_NAME
97                        ": IDE interface at GenBus disabled\n");
98                 return -EBUSY;
99         }
100
101         printk(KERN_INFO DRV_NAME ": IDE interface at GenBus slot %i\n",
102                IDE_CS);
103
104         swarm_ide_resource.start = offset;
105         swarm_ide_resource.end = offset + size - 1;
106         if (request_resource(&iomem_resource, &swarm_ide_resource)) {
107                 printk(KERN_ERR DRV_NAME
108                        ": can't request I/O memory resource\n");
109                 return -EBUSY;
110         }
111
112         base = ioremap(offset, size);
113
114         /* Setup MMIO ops.  */
115         default_hwif_mmiops(hwif);
116         /* Prevent resource map manipulation.  */
117         hwif->mmio = 2;
118         hwif->noprobe = 0;
119
120         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
121                 hwif->hw.io_ports[i] =
122                                 (unsigned long)(base + ((0x1f0 + i) << 5));
123         hwif->hw.io_ports[IDE_CONTROL_OFFSET] =
124                                 (unsigned long)(base + (0x3f6 << 5));
125         hwif->hw.irq = K_INT_GB_IDE;
126
127         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
128         hwif->irq = hwif->hw.irq;
129
130         dev_set_drvdata(dev, hwif);
131
132         return 0;
133 }
134
135 static struct device_driver swarm_ide_driver = {
136         .name   = swarm_ide_string,
137         .bus    = &platform_bus_type,
138         .probe  = swarm_ide_probe,
139 };
140
141 static void swarm_ide_platform_release(struct device *device)
142 {
143         struct platform_device *pldev;
144
145         /* free device */
146         pldev = to_platform_device(device);
147         kfree(pldev);
148 }
149
150 static int __devinit swarm_ide_init_module(void)
151 {
152         struct platform_device *pldev;
153         int err;
154
155         printk(KERN_INFO "SWARM IDE driver\n");
156
157         if (driver_register(&swarm_ide_driver)) {
158                 printk(KERN_ERR "Driver registration failed\n");
159                 err = -ENODEV;
160                 goto out;
161         }
162
163         if (!(pldev = kmalloc(sizeof (*pldev), GFP_KERNEL))) {
164                 err = -ENOMEM;
165                 goto out_unregister_driver;
166         }
167
168         memset (pldev, 0, sizeof (*pldev));
169         pldev->name             = swarm_ide_string;
170         pldev->id               = 0;
171         pldev->dev.release      = swarm_ide_platform_release;
172
173         if (platform_device_register(pldev)) {
174                 err = -ENODEV;
175                 goto out_free_pldev;
176         }
177
178         if (!pldev->dev.driver) {
179                 /*
180                  * The driver was not bound to this device, there was
181                  * no hardware at this address. Unregister it, as the
182                  * release fuction will take care of freeing the
183                  * allocated structure
184                  */
185                 platform_device_unregister (pldev);
186         }
187
188         swarm_ide_dev = pldev;
189
190         return 0;
191
192 out_free_pldev:
193         kfree(pldev);
194
195 out_unregister_driver:
196         driver_unregister(&swarm_ide_driver);
197 out:
198         return err;
199 }
200
201 module_init(swarm_ide_init_module);