blob: 2f2b4f4cf229f9e046b3dec3b83e367fd0038efd [file] [log] [blame]
Anton Salnikov7c7e92a2008-02-06 02:57:48 +01001/*
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 */
46struct 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
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +020079static unsigned ideclk_period; /* in nanoseconds */
Anton Salnikov7c7e92a2008-02-06 02:57:48 +010080
81static 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 */
87};
88
Anton Salnikov7c7e92a2008-02-06 02:57:48 +010089static 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 */
Julia Lawall00fe8b72008-04-26 17:36:35 +020097 t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +020098 ideclk_period) - 1;
99 tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
Julia Lawall00fe8b72008-04-26 17:36:35 +0200100 trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200101 ideclk_period) - 1;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100102
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
128static 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 */
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200142 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
143 td = DIV_ROUND_UP(t->active, ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100144 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
160static 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 */
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200169 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
Julia Lawall00fe8b72008-04-26 17:36:35 +0200170 t2 = DIV_ROUND_UP(ide_timing_find_mode(XFER_PIO_0 + mode)->active,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200171 ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100172
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);
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200193 t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period);
194 t2 = DIV_ROUND_UP(t->act8b, ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100195
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
208static 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
222static 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
237static 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}
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100312
313static u8 __devinit palm_bk3710_cable_detect(ide_hwif_t *hwif)
314{
315 return ATA_CBL_PATA80;
316}
317
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200318static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif,
319 const struct ide_port_info *d)
320{
321 unsigned long base =
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200322 hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200323
324 printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
325
326 if (ide_allocate_dma_engine(hwif))
327 return -1;
328
Bartlomiej Zolnierkiewiczf37afda2008-04-26 22:25:24 +0200329 ide_setup_dma(hwif, base);
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200330
331 return 0;
332}
333
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200334static 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};
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100339
340static const struct ide_port_info __devinitdata palm_bk3710_port_info = {
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200341 .init_dma = palm_bk3710_init_dma,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200342 .port_ops = &palm_bk3710_ports_ops,
Bartlomiej Zolnierkiewiczc5dd43e2008-04-28 23:44:37 +0200343 .host_flags = IDE_HFLAG_MMIO,
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100344 .pio_mask = ATA_PIO4,
345 .udma_mask = ATA_UDMA4, /* (input clk 99MHz) */
346 .mwdma_mask = ATA_MWDMA2,
347};
348
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100349static int __devinit palm_bk3710_probe(struct platform_device *pdev)
350{
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200351 struct clk *clk;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100352 struct resource *mem, *irq;
353 ide_hwif_t *hwif;
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200354 unsigned long base, rate;
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200355 int i;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100356 hw_regs_t hw;
357 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100358
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200359 clk = clk_get(NULL, "IDECLK");
360 if (IS_ERR(clk))
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100361 return -ENODEV;
362
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200363 clk_enable(clk);
364 rate = clk_get_rate(clk);
365 ideclk_period = 1000000000UL / rate;
366
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100367 /* Register the IDE interface with Linux ATA Interface */
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100368 memset(&hw, 0, sizeof(hw));
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100369
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 }
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200375
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100376 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
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200382 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);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100389
390 /* Configure the Palm Chip controller */
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200391 palm_bk3710_chipinit((void __iomem *)base);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100392
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100393 for (i = 0; i < IDE_NR_PORTS - 2; i++)
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200394 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;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100396 hw.irq = irq->start;
397 hw.chipset = ide_palm3710;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100398
Bartlomiej Zolnierkiewicz59bff5b2008-04-26 17:36:32 +0200399 hwif = ide_find_port();
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100400 if (hwif == NULL)
401 goto out;
402
403 i = hwif->index;
404
Bartlomiej Zolnierkiewiczbf64b7a2008-04-27 15:38:31 +0200405 ide_init_port_data(hwif, i);
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100406 ide_init_port_hw(hwif, &hw);
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100407
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100408 hwif->mmio = 1;
409 default_hwif_mmiops(hwif);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100410
Bartlomiej Zolnierkiewiczc92a7f12008-02-11 00:32:13 +0100411 idx[0] = i;
412
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100413 ide_device_add(idx, &palm_bk3710_port_info);
Bartlomiej Zolnierkiewiczc92a7f12008-02-11 00:32:13 +0100414
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100415 return 0;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100416out:
417 printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
418 return -ENODEV;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100419}
420
Kay Sievers458622f2008-04-18 13:41:57 -0700421/* work with hotplug and coldplug */
422MODULE_ALIAS("platform:palm_bk3710");
423
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100424static struct platform_driver platform_bk_driver = {
425 .driver = {
426 .name = "palm_bk3710",
Kay Sievers458622f2008-04-18 13:41:57 -0700427 .owner = THIS_MODULE,
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100428 },
429 .probe = palm_bk3710_probe,
430 .remove = NULL,
431};
432
433static int __init palm_bk3710_init(void)
434{
435 return platform_driver_register(&platform_bk_driver);
436}
437
438module_init(palm_bk3710_init);
439MODULE_LICENSE("GPL");