Merge branch 'release-2.6.27' of git://git.kernel.org/pub/scm/linux/kernel/git/ak...
[pandora-kernel.git] / drivers / ide / arm / palm_bk3710.c
1 /*
2  * Palmchip bk3710 IDE controller
3  *
4  * Copyright (C) 2006 Texas Instruments.
5  * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6  *
7  * ----------------------------------------------------------------------------
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  * ----------------------------------------------------------------------------
23  *
24  */
25
26 #include <linux/types.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/ioport.h>
30 #include <linux/hdreg.h>
31 #include <linux/ide.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/clk.h>
35 #include <linux/platform_device.h>
36
37 /* Offset of the primary interface registers */
38 #define IDE_PALM_ATA_PRI_REG_OFFSET 0x1F0
39
40 /* Primary Control Offset */
41 #define IDE_PALM_ATA_PRI_CTL_OFFSET 0x3F6
42
43 /*
44  * PalmChip 3710 IDE Controller UDMA timing structure Definition
45  */
46 struct palm_bk3710_udmatiming {
47         unsigned int rptime;    /* Ready to pause time  */
48         unsigned int cycletime; /* Cycle Time           */
49 };
50
51 #define BK3710_BMICP            0x00
52 #define BK3710_BMISP            0x02
53 #define BK3710_BMIDTP           0x04
54 #define BK3710_BMICS            0x08
55 #define BK3710_BMISS            0x0A
56 #define BK3710_BMIDTS           0x0C
57 #define BK3710_IDETIMP          0x40
58 #define BK3710_IDETIMS          0x42
59 #define BK3710_SIDETIM          0x44
60 #define BK3710_SLEWCTL          0x45
61 #define BK3710_IDESTATUS        0x47
62 #define BK3710_UDMACTL          0x48
63 #define BK3710_UDMATIM          0x4A
64 #define BK3710_MISCCTL          0x50
65 #define BK3710_REGSTB           0x54
66 #define BK3710_REGRCVR          0x58
67 #define BK3710_DATSTB           0x5C
68 #define BK3710_DATRCVR          0x60
69 #define BK3710_DMASTB           0x64
70 #define BK3710_DMARCVR          0x68
71 #define BK3710_UDMASTB          0x6C
72 #define BK3710_UDMATRP          0x70
73 #define BK3710_UDMAENV          0x74
74 #define BK3710_IORDYTMP         0x78
75 #define BK3710_IORDYTMS         0x7C
76
77 #include "../ide-timing.h"
78
79 static unsigned ideclk_period; /* in nanoseconds */
80
81 static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = {
82         {160, 240},             /* UDMA Mode 0 */
83         {125, 160},             /* UDMA Mode 1 */
84         {100, 120},             /* UDMA Mode 2 */
85         {100, 90},              /* UDMA Mode 3 */
86         {100, 60},              /* UDMA Mode 4 */
87 };
88
89 static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev,
90                                     unsigned int mode)
91 {
92         u8 tenv, trp, t0;
93         u32 val32;
94         u16 val16;
95
96         /* DMA Data Setup */
97         t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
98                           ideclk_period) - 1;
99         tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
100         trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
101                            ideclk_period) - 1;
102
103         /* udmatim Register */
104         val16 = readw(base + BK3710_UDMATIM) & (dev ? 0xFF0F : 0xFFF0);
105         val16 |= (mode << (dev ? 4 : 0));
106         writew(val16, base + BK3710_UDMATIM);
107
108         /* udmastb Ultra DMA Access Strobe Width */
109         val32 = readl(base + BK3710_UDMASTB) & (0xFF << (dev ? 0 : 8));
110         val32 |= (t0 << (dev ? 8 : 0));
111         writel(val32, base + BK3710_UDMASTB);
112
113         /* udmatrp Ultra DMA Ready to Pause Time */
114         val32 = readl(base + BK3710_UDMATRP) & (0xFF << (dev ? 0 : 8));
115         val32 |= (trp << (dev ? 8 : 0));
116         writel(val32, base + BK3710_UDMATRP);
117
118         /* udmaenv Ultra DMA envelop Time */
119         val32 = readl(base + BK3710_UDMAENV) & (0xFF << (dev ? 0 : 8));
120         val32 |= (tenv << (dev ? 8 : 0));
121         writel(val32, base + BK3710_UDMAENV);
122
123         /* Enable UDMA for Device */
124         val16 = readw(base + BK3710_UDMACTL) | (1 << dev);
125         writew(val16, base + BK3710_UDMACTL);
126 }
127
128 static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev,
129                                    unsigned short min_cycle,
130                                    unsigned int mode)
131 {
132         u8 td, tkw, t0;
133         u32 val32;
134         u16 val16;
135         struct ide_timing *t;
136         int cycletime;
137
138         t = ide_timing_find_mode(mode);
139         cycletime = max_t(int, t->cycle, min_cycle);
140
141         /* DMA Data Setup */
142         t0 = DIV_ROUND_UP(cycletime, ideclk_period);
143         td = DIV_ROUND_UP(t->active, ideclk_period);
144         tkw = t0 - td - 1;
145         td -= 1;
146
147         val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8));
148         val32 |= (td << (dev ? 8 : 0));
149         writel(val32, base + BK3710_DMASTB);
150
151         val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8));
152         val32 |= (tkw << (dev ? 8 : 0));
153         writel(val32, base + BK3710_DMARCVR);
154
155         /* Disable UDMA for Device */
156         val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev);
157         writew(val16, base + BK3710_UDMACTL);
158 }
159
160 static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate,
161                                    unsigned int dev, unsigned int cycletime,
162                                    unsigned int mode)
163 {
164         u8 t2, t2i, t0;
165         u32 val32;
166         struct ide_timing *t;
167
168         /* PIO Data Setup */
169         t0 = DIV_ROUND_UP(cycletime, ideclk_period);
170         t2 = DIV_ROUND_UP(ide_timing_find_mode(XFER_PIO_0 + mode)->active,
171                           ideclk_period);
172
173         t2i = t0 - t2 - 1;
174         t2 -= 1;
175
176         val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8));
177         val32 |= (t2 << (dev ? 8 : 0));
178         writel(val32, base + BK3710_DATSTB);
179
180         val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8));
181         val32 |= (t2i << (dev ? 8 : 0));
182         writel(val32, base + BK3710_DATRCVR);
183
184         if (mate && mate->present) {
185                 u8 mode2 = ide_get_best_pio_mode(mate, 255, 4);
186
187                 if (mode2 < mode)
188                         mode = mode2;
189         }
190
191         /* TASKFILE Setup */
192         t = ide_timing_find_mode(XFER_PIO_0 + mode);
193         t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period);
194         t2 = DIV_ROUND_UP(t->act8b, ideclk_period);
195
196         t2i = t0 - t2 - 1;
197         t2 -= 1;
198
199         val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8));
200         val32 |= (t2 << (dev ? 8 : 0));
201         writel(val32, base + BK3710_REGSTB);
202
203         val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8));
204         val32 |= (t2i << (dev ? 8 : 0));
205         writel(val32, base + BK3710_REGRCVR);
206 }
207
208 static void palm_bk3710_set_dma_mode(ide_drive_t *drive, u8 xferspeed)
209 {
210         int is_slave = drive->dn & 1;
211         void __iomem *base = (void *)drive->hwif->dma_base;
212
213         if (xferspeed >= XFER_UDMA_0) {
214                 palm_bk3710_setudmamode(base, is_slave,
215                                         xferspeed - XFER_UDMA_0);
216         } else {
217                 palm_bk3710_setdmamode(base, is_slave, drive->id->eide_dma_min,
218                                        xferspeed);
219         }
220 }
221
222 static void palm_bk3710_set_pio_mode(ide_drive_t *drive, u8 pio)
223 {
224         unsigned int cycle_time;
225         int is_slave = drive->dn & 1;
226         ide_drive_t *mate;
227         void __iomem *base = (void *)drive->hwif->dma_base;
228
229         /*
230          * Obtain the drive PIO data for tuning the Palm Chip registers
231          */
232         cycle_time = ide_pio_cycle_time(drive, pio);
233         mate = ide_get_paired_drive(drive);
234         palm_bk3710_setpiomode(base, mate, is_slave, cycle_time, pio);
235 }
236
237 static void __devinit palm_bk3710_chipinit(void __iomem *base)
238 {
239         /*
240          * enable the reset_en of ATA controller so that when ata signals
241          * are brought out, by writing into device config. at that
242          * time por_n signal should not be 'Z' and have a stable value.
243          */
244         writel(0x0300, base + BK3710_MISCCTL);
245
246         /* wait for some time and deassert the reset of ATA Device. */
247         mdelay(100);
248
249         /* Deassert the Reset */
250         writel(0x0200, base + BK3710_MISCCTL);
251
252         /*
253          * Program the IDETIMP Register Value based on the following assumptions
254          *
255          * (ATA_IDETIMP_IDEEN           , ENABLE ) |
256          * (ATA_IDETIMP_SLVTIMEN        , DISABLE) |
257          * (ATA_IDETIMP_RDYSMPL         , 70NS)    |
258          * (ATA_IDETIMP_RDYRCVRY        , 50NS)    |
259          * (ATA_IDETIMP_DMAFTIM1        , PIOCOMP) |
260          * (ATA_IDETIMP_PREPOST1        , DISABLE) |
261          * (ATA_IDETIMP_RDYSEN1         , DISABLE) |
262          * (ATA_IDETIMP_PIOFTIM1        , DISABLE) |
263          * (ATA_IDETIMP_DMAFTIM0        , PIOCOMP) |
264          * (ATA_IDETIMP_PREPOST0        , DISABLE) |
265          * (ATA_IDETIMP_RDYSEN0         , DISABLE) |
266          * (ATA_IDETIMP_PIOFTIM0        , DISABLE)
267          */
268         writew(0xB388, base + BK3710_IDETIMP);
269
270         /*
271          * Configure  SIDETIM  Register
272          * (ATA_SIDETIM_RDYSMPS1        ,120NS ) |
273          * (ATA_SIDETIM_RDYRCYS1        ,120NS )
274          */
275         writeb(0, base + BK3710_SIDETIM);
276
277         /*
278          * UDMACTL Ultra-ATA DMA Control
279          * (ATA_UDMACTL_UDMAP1  , 0 ) |
280          * (ATA_UDMACTL_UDMAP0  , 0 )
281          *
282          */
283         writew(0, base + BK3710_UDMACTL);
284
285         /*
286          * MISCCTL Miscellaneous Conrol Register
287          * (ATA_MISCCTL_RSTMODEP        , 1) |
288          * (ATA_MISCCTL_RESETP          , 0) |
289          * (ATA_MISCCTL_TIMORIDE        , 1)
290          */
291         writel(0x201, base + BK3710_MISCCTL);
292
293         /*
294          * IORDYTMP IORDY Timer for Primary Register
295          * (ATA_IORDYTMP_IORDYTMP     , 0xffff  )
296          */
297         writel(0xFFFF, base + BK3710_IORDYTMP);
298
299         /*
300          * Configure BMISP Register
301          * (ATA_BMISP_DMAEN1    , DISABLE )     |
302          * (ATA_BMISP_DMAEN0    , DISABLE )     |
303          * (ATA_BMISP_IORDYINT  , CLEAR)        |
304          * (ATA_BMISP_INTRSTAT  , CLEAR)        |
305          * (ATA_BMISP_DMAERROR  , CLEAR)
306          */
307         writew(0, base + BK3710_BMISP);
308
309         palm_bk3710_setpiomode(base, NULL, 0, 600, 0);
310         palm_bk3710_setpiomode(base, NULL, 1, 600, 0);
311 }
312
313 static u8 __devinit palm_bk3710_cable_detect(ide_hwif_t *hwif)
314 {
315         return ATA_CBL_PATA80;
316 }
317
318 static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif,
319                                           const struct ide_port_info *d)
320 {
321         unsigned long base =
322                 hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
323
324         printk(KERN_INFO "    %s: MMIO-DMA\n", hwif->name);
325
326         if (ide_allocate_dma_engine(hwif))
327                 return -1;
328
329         ide_setup_dma(hwif, base);
330
331         return 0;
332 }
333
334 static const struct ide_port_ops palm_bk3710_ports_ops = {
335         .set_pio_mode           = palm_bk3710_set_pio_mode,
336         .set_dma_mode           = palm_bk3710_set_dma_mode,
337         .cable_detect           = palm_bk3710_cable_detect,
338 };
339
340 static const struct ide_port_info __devinitdata palm_bk3710_port_info = {
341         .init_dma               = palm_bk3710_init_dma,
342         .port_ops               = &palm_bk3710_ports_ops,
343         .host_flags             = IDE_HFLAG_MMIO,
344         .pio_mask               = ATA_PIO4,
345         .udma_mask              = ATA_UDMA4,    /* (input clk 99MHz) */
346         .mwdma_mask             = ATA_MWDMA2,
347 };
348
349 static int __devinit palm_bk3710_probe(struct platform_device *pdev)
350 {
351         struct clk *clk;
352         struct resource *mem, *irq;
353         ide_hwif_t *hwif;
354         unsigned long base, rate;
355         int i;
356         hw_regs_t hw;
357         u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
358
359         clk = clk_get(NULL, "IDECLK");
360         if (IS_ERR(clk))
361                 return -ENODEV;
362
363         clk_enable(clk);
364         rate = clk_get_rate(clk);
365         ideclk_period = 1000000000UL / rate;
366
367         /* Register the IDE interface with Linux ATA Interface */
368         memset(&hw, 0, sizeof(hw));
369
370         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
371         if (mem == NULL) {
372                 printk(KERN_ERR "failed to get memory region resource\n");
373                 return -ENODEV;
374         }
375
376         irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
377         if (irq == NULL) {
378                 printk(KERN_ERR "failed to get IRQ resource\n");
379                 return -ENODEV;
380         }
381
382         if (request_mem_region(mem->start, mem->end - mem->start + 1,
383                                "palm_bk3710") == NULL) {
384                 printk(KERN_ERR "failed to request memory region\n");
385                 return -EBUSY;
386         }
387
388         base = IO_ADDRESS(mem->start);
389
390         /* Configure the Palm Chip controller */
391         palm_bk3710_chipinit((void __iomem *)base);
392
393         for (i = 0; i < IDE_NR_PORTS - 2; i++)
394                 hw.io_ports_array[i] = base + IDE_PALM_ATA_PRI_REG_OFFSET + i;
395         hw.io_ports.ctl_addr = base + IDE_PALM_ATA_PRI_CTL_OFFSET;
396         hw.irq = irq->start;
397         hw.chipset = ide_palm3710;
398
399         hwif = ide_find_port();
400         if (hwif == NULL)
401                 goto out;
402
403         i = hwif->index;
404
405         ide_init_port_data(hwif, i);
406         ide_init_port_hw(hwif, &hw);
407
408         default_hwif_mmiops(hwif);
409
410         idx[0] = i;
411
412         ide_device_add(idx, &palm_bk3710_port_info);
413
414         return 0;
415 out:
416         printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
417         return -ENODEV;
418 }
419
420 /* work with hotplug and coldplug */
421 MODULE_ALIAS("platform:palm_bk3710");
422
423 static struct platform_driver platform_bk_driver = {
424         .driver = {
425                 .name = "palm_bk3710",
426                 .owner = THIS_MODULE,
427         },
428         .probe = palm_bk3710_probe,
429         .remove = NULL,
430 };
431
432 static int __init palm_bk3710_init(void)
433 {
434         return platform_driver_register(&platform_bk_driver);
435 }
436
437 module_init(palm_bk3710_init);
438 MODULE_LICENSE("GPL");