2 * Palmchip bk3710 IDE controller
4 * Copyright (C) 2006 Texas Instruments.
5 * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
7 * ----------------------------------------------------------------------------
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.
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.
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 * ----------------------------------------------------------------------------
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>
37 /* Offset of the primary interface registers */
38 #define IDE_PALM_ATA_PRI_REG_OFFSET 0x1F0
40 /* Primary Control Offset */
41 #define IDE_PALM_ATA_PRI_CTL_OFFSET 0x3F6
44 * PalmChip 3710 IDE Controller UDMA timing structure Definition
46 struct palm_bk3710_udmatiming {
47 unsigned int rptime; /* Ready to pause time */
48 unsigned int cycletime; /* Cycle Time */
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
77 #include "../ide-timing.h"
79 static unsigned ideclk_period; /* in nanoseconds */
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 {85, 60}, /* UDMA Mode 4 */
89 static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev,
97 t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
99 tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
100 trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
103 /* udmatim Register */
104 val16 = readw(base + BK3710_UDMATIM) & (dev ? 0xFF0F : 0xFFF0);
105 val16 |= (mode << (dev ? 4 : 0));
106 writew(val16, base + BK3710_UDMATIM);
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);
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);
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);
123 /* Enable UDMA for Device */
124 val16 = readw(base + BK3710_UDMACTL) | (1 << dev);
125 writew(val16, base + BK3710_UDMACTL);
128 static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev,
129 unsigned short min_cycle,
135 struct ide_timing *t;
138 t = ide_timing_find_mode(mode);
139 cycletime = max_t(int, t->cycle, min_cycle);
142 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
143 td = DIV_ROUND_UP(t->active, ideclk_period);
147 val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8));
148 val32 |= (td << (dev ? 8 : 0));
149 writel(val32, base + BK3710_DMASTB);
151 val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8));
152 val32 |= (tkw << (dev ? 8 : 0));
153 writel(val32, base + BK3710_DMARCVR);
155 /* Disable UDMA for Device */
156 val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev);
157 writew(val16, base + BK3710_UDMACTL);
160 static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate,
161 unsigned int dev, unsigned int cycletime,
166 struct ide_timing *t;
169 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
170 t2 = DIV_ROUND_UP(ide_timing_find_mode(XFER_PIO_0 + mode)->active,
176 val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8));
177 val32 |= (t2 << (dev ? 8 : 0));
178 writel(val32, base + BK3710_DATSTB);
180 val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8));
181 val32 |= (t2i << (dev ? 8 : 0));
182 writel(val32, base + BK3710_DATRCVR);
184 if (mate && mate->present) {
185 u8 mode2 = ide_get_best_pio_mode(mate, 255, 4);
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);
199 val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8));
200 val32 |= (t2 << (dev ? 8 : 0));
201 writel(val32, base + BK3710_REGSTB);
203 val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8));
204 val32 |= (t2i << (dev ? 8 : 0));
205 writel(val32, base + BK3710_REGRCVR);
208 static void palm_bk3710_set_dma_mode(ide_drive_t *drive, u8 xferspeed)
210 int is_slave = drive->dn & 1;
211 void __iomem *base = (void *)drive->hwif->dma_base;
213 if (xferspeed >= XFER_UDMA_0) {
214 palm_bk3710_setudmamode(base, is_slave,
215 xferspeed - XFER_UDMA_0);
217 palm_bk3710_setdmamode(base, is_slave, drive->id->eide_dma_min,
222 static void palm_bk3710_set_pio_mode(ide_drive_t *drive, u8 pio)
224 unsigned int cycle_time;
225 int is_slave = drive->dn & 1;
227 void __iomem *base = (void *)drive->hwif->dma_base;
230 * Obtain the drive PIO data for tuning the Palm Chip registers
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);
237 static void __devinit palm_bk3710_chipinit(void __iomem *base)
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.
244 writel(0x0300, base + BK3710_MISCCTL);
246 /* wait for some time and deassert the reset of ATA Device. */
249 /* Deassert the Reset */
250 writel(0x0200, base + BK3710_MISCCTL);
253 * Program the IDETIMP Register Value based on the following assumptions
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)
268 writew(0xB388, base + BK3710_IDETIMP);
271 * Configure SIDETIM Register
272 * (ATA_SIDETIM_RDYSMPS1 ,120NS ) |
273 * (ATA_SIDETIM_RDYRCYS1 ,120NS )
275 writeb(0, base + BK3710_SIDETIM);
278 * UDMACTL Ultra-ATA DMA Control
279 * (ATA_UDMACTL_UDMAP1 , 0 ) |
280 * (ATA_UDMACTL_UDMAP0 , 0 )
283 writew(0, base + BK3710_UDMACTL);
286 * MISCCTL Miscellaneous Conrol Register
287 * (ATA_MISCCTL_RSTMODEP , 1) |
288 * (ATA_MISCCTL_RESETP , 0) |
289 * (ATA_MISCCTL_TIMORIDE , 1)
291 writel(0x201, base + BK3710_MISCCTL);
294 * IORDYTMP IORDY Timer for Primary Register
295 * (ATA_IORDYTMP_IORDYTMP , 0xffff )
297 writel(0xFFFF, base + BK3710_IORDYTMP);
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)
307 writew(0, base + BK3710_BMISP);
309 palm_bk3710_setpiomode(base, NULL, 0, 600, 0);
310 palm_bk3710_setpiomode(base, NULL, 1, 600, 0);
313 static u8 __devinit palm_bk3710_cable_detect(ide_hwif_t *hwif)
315 return ATA_CBL_PATA80;
318 static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif,
319 const struct ide_port_info *d)
322 hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
324 printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
326 if (ide_allocate_dma_engine(hwif))
329 ide_setup_dma(hwif, base);
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,
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,
349 static int __devinit palm_bk3710_probe(struct platform_device *pdev)
352 struct resource *mem, *irq;
354 unsigned long base, rate;
357 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
359 clk = clk_get(NULL, "IDECLK");
364 rate = clk_get_rate(clk);
365 ideclk_period = 1000000000UL / rate;
367 /* Register the IDE interface with Linux ATA Interface */
368 memset(&hw, 0, sizeof(hw));
370 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
372 printk(KERN_ERR "failed to get memory region resource\n");
376 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
378 printk(KERN_ERR "failed to get IRQ resource\n");
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");
388 base = IO_ADDRESS(mem->start);
390 /* Configure the Palm Chip controller */
391 palm_bk3710_chipinit((void __iomem *)base);
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;
397 hw.chipset = ide_palm3710;
399 hwif = ide_find_port();
405 ide_init_port_data(hwif, i);
406 ide_init_port_hw(hwif, &hw);
409 default_hwif_mmiops(hwif);
413 ide_device_add(idx, &palm_bk3710_port_info);
417 printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
421 /* work with hotplug and coldplug */
422 MODULE_ALIAS("platform:palm_bk3710");
424 static struct platform_driver platform_bk_driver = {
426 .name = "palm_bk3710",
427 .owner = THIS_MODULE,
429 .probe = palm_bk3710_probe,
433 static int __init palm_bk3710_init(void)
435 return platform_driver_register(&platform_bk_driver);
438 module_init(palm_bk3710_init);
439 MODULE_LICENSE("GPL");