blob: 320971c78cb7e7521f3134d9bb30da849f618131 [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
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +020077static unsigned ideclk_period; /* in nanoseconds */
Anton Salnikov7c7e92a2008-02-06 02:57:48 +010078
79static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = {
80 {160, 240}, /* UDMA Mode 0 */
81 {125, 160}, /* UDMA Mode 1 */
82 {100, 120}, /* UDMA Mode 2 */
83 {100, 90}, /* UDMA Mode 3 */
Mikhail Cherkashin8c6e46d2008-07-15 21:21:40 +020084 {100, 60}, /* UDMA Mode 4 */
Sergei Shtylyova0f403b2008-07-24 22:53:34 +020085 {85, 40}, /* UDMA Mode 5 */
Anton Salnikov7c7e92a2008-02-06 02:57:48 +010086};
87
Anton Salnikov7c7e92a2008-02-06 02:57:48 +010088static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev,
89 unsigned int mode)
90{
91 u8 tenv, trp, t0;
92 u32 val32;
93 u16 val16;
94
95 /* DMA Data Setup */
Julia Lawall00fe8b72008-04-26 17:36:35 +020096 t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +020097 ideclk_period) - 1;
98 tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
Julia Lawall00fe8b72008-04-26 17:36:35 +020099 trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200100 ideclk_period) - 1;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100101
102 /* udmatim Register */
103 val16 = readw(base + BK3710_UDMATIM) & (dev ? 0xFF0F : 0xFFF0);
104 val16 |= (mode << (dev ? 4 : 0));
105 writew(val16, base + BK3710_UDMATIM);
106
107 /* udmastb Ultra DMA Access Strobe Width */
108 val32 = readl(base + BK3710_UDMASTB) & (0xFF << (dev ? 0 : 8));
109 val32 |= (t0 << (dev ? 8 : 0));
110 writel(val32, base + BK3710_UDMASTB);
111
112 /* udmatrp Ultra DMA Ready to Pause Time */
113 val32 = readl(base + BK3710_UDMATRP) & (0xFF << (dev ? 0 : 8));
114 val32 |= (trp << (dev ? 8 : 0));
115 writel(val32, base + BK3710_UDMATRP);
116
117 /* udmaenv Ultra DMA envelop Time */
118 val32 = readl(base + BK3710_UDMAENV) & (0xFF << (dev ? 0 : 8));
119 val32 |= (tenv << (dev ? 8 : 0));
120 writel(val32, base + BK3710_UDMAENV);
121
122 /* Enable UDMA for Device */
123 val16 = readw(base + BK3710_UDMACTL) | (1 << dev);
124 writew(val16, base + BK3710_UDMACTL);
125}
126
127static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev,
128 unsigned short min_cycle,
129 unsigned int mode)
130{
131 u8 td, tkw, t0;
132 u32 val32;
133 u16 val16;
134 struct ide_timing *t;
135 int cycletime;
136
137 t = ide_timing_find_mode(mode);
138 cycletime = max_t(int, t->cycle, min_cycle);
139
140 /* DMA Data Setup */
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200141 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
142 td = DIV_ROUND_UP(t->active, ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100143 tkw = t0 - td - 1;
144 td -= 1;
145
146 val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8));
147 val32 |= (td << (dev ? 8 : 0));
148 writel(val32, base + BK3710_DMASTB);
149
150 val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8));
151 val32 |= (tkw << (dev ? 8 : 0));
152 writel(val32, base + BK3710_DMARCVR);
153
154 /* Disable UDMA for Device */
155 val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev);
156 writew(val16, base + BK3710_UDMACTL);
157}
158
159static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate,
160 unsigned int dev, unsigned int cycletime,
161 unsigned int mode)
162{
163 u8 t2, t2i, t0;
164 u32 val32;
165 struct ide_timing *t;
166
167 /* PIO Data Setup */
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200168 t0 = DIV_ROUND_UP(cycletime, ideclk_period);
Julia Lawall00fe8b72008-04-26 17:36:35 +0200169 t2 = DIV_ROUND_UP(ide_timing_find_mode(XFER_PIO_0 + mode)->active,
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200170 ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100171
172 t2i = t0 - t2 - 1;
173 t2 -= 1;
174
175 val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8));
176 val32 |= (t2 << (dev ? 8 : 0));
177 writel(val32, base + BK3710_DATSTB);
178
179 val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8));
180 val32 |= (t2i << (dev ? 8 : 0));
181 writel(val32, base + BK3710_DATRCVR);
182
Bartlomiej Zolnierkiewicz7e59ea22008-10-10 22:39:26 +0200183 if (mate) {
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100184 u8 mode2 = ide_get_best_pio_mode(mate, 255, 4);
185
186 if (mode2 < mode)
187 mode = mode2;
188 }
189
190 /* TASKFILE Setup */
191 t = ide_timing_find_mode(XFER_PIO_0 + mode);
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200192 t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period);
193 t2 = DIV_ROUND_UP(t->act8b, ideclk_period);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100194
195 t2i = t0 - t2 - 1;
196 t2 -= 1;
197
198 val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8));
199 val32 |= (t2 << (dev ? 8 : 0));
200 writel(val32, base + BK3710_REGSTB);
201
202 val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8));
203 val32 |= (t2i << (dev ? 8 : 0));
204 writel(val32, base + BK3710_REGRCVR);
205}
206
207static void palm_bk3710_set_dma_mode(ide_drive_t *drive, u8 xferspeed)
208{
209 int is_slave = drive->dn & 1;
210 void __iomem *base = (void *)drive->hwif->dma_base;
211
212 if (xferspeed >= XFER_UDMA_0) {
213 palm_bk3710_setudmamode(base, is_slave,
214 xferspeed - XFER_UDMA_0);
215 } else {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200216 palm_bk3710_setdmamode(base, is_slave,
217 drive->id[ATA_ID_EIDE_DMA_MIN],
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100218 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);
Bartlomiej Zolnierkiewicz7e59ea22008-10-10 22:39:26 +0200233 mate = ide_get_pair_dev(drive);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100234 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
Bartlomiej Zolnierkiewiczf454cbe2008-08-05 18:17:04 +0200313static u8 palm_bk3710_cable_detect(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100314{
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{
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200321 printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
322
323 if (ide_allocate_dma_engine(hwif))
324 return -1;
325
Bartlomiej Zolnierkiewicz81e8d5a2008-07-23 19:55:51 +0200326 hwif->dma_base = hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
327
328 hwif->dma_ops = &sff_dma_ops;
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200329
330 return 0;
331}
332
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200333static const struct ide_port_ops palm_bk3710_ports_ops = {
334 .set_pio_mode = palm_bk3710_set_pio_mode,
335 .set_dma_mode = palm_bk3710_set_dma_mode,
336 .cable_detect = palm_bk3710_cable_detect,
337};
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100338
Sergei Shtylyova0f403b2008-07-24 22:53:34 +0200339static struct ide_port_info __devinitdata palm_bk3710_port_info = {
Bartlomiej Zolnierkiewiczb552a2c2008-04-26 22:25:23 +0200340 .init_dma = palm_bk3710_init_dma,
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200341 .port_ops = &palm_bk3710_ports_ops,
Bartlomiej Zolnierkiewiczc5dd43e2008-04-28 23:44:37 +0200342 .host_flags = IDE_HFLAG_MMIO,
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100343 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewiczc79b60d2008-02-11 00:32:13 +0100344 .mwdma_mask = ATA_MWDMA2,
345};
346
David Brownellbfc2f012008-09-02 20:18:47 +0200347static int __init palm_bk3710_probe(struct platform_device *pdev)
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100348{
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200349 struct clk *clk;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100350 struct resource *mem, *irq;
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200351 unsigned long base, rate;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +0200352 int i, rc;
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +0200353 hw_regs_t hw, *hws[] = { &hw, NULL, NULL, NULL };
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100354
Sergei Shtylyova0f403b2008-07-24 22:53:34 +0200355 clk = clk_get(&pdev->dev, "IDECLK");
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200356 if (IS_ERR(clk))
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100357 return -ENODEV;
358
Sergei Shtylyovffab6cf2008-07-08 19:27:22 +0200359 clk_enable(clk);
360 rate = clk_get_rate(clk);
361 ideclk_period = 1000000000UL / rate;
362
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100363 /* Register the IDE interface with Linux ATA Interface */
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100364 memset(&hw, 0, sizeof(hw));
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100365
366 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
367 if (mem == NULL) {
368 printk(KERN_ERR "failed to get memory region resource\n");
369 return -ENODEV;
370 }
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200371
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100372 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
373 if (irq == NULL) {
374 printk(KERN_ERR "failed to get IRQ resource\n");
375 return -ENODEV;
376 }
377
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200378 if (request_mem_region(mem->start, mem->end - mem->start + 1,
379 "palm_bk3710") == NULL) {
380 printk(KERN_ERR "failed to request memory region\n");
381 return -EBUSY;
382 }
383
384 base = IO_ADDRESS(mem->start);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100385
386 /* Configure the Palm Chip controller */
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200387 palm_bk3710_chipinit((void __iomem *)base);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100388
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100389 for (i = 0; i < IDE_NR_PORTS - 2; i++)
Sergei Shtylyovce42a542008-06-20 20:53:32 +0200390 hw.io_ports_array[i] = base + IDE_PALM_ATA_PRI_REG_OFFSET + i;
391 hw.io_ports.ctl_addr = base + IDE_PALM_ATA_PRI_CTL_OFFSET;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100392 hw.irq = irq->start;
David Brownellbfc2f012008-09-02 20:18:47 +0200393 hw.dev = &pdev->dev;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100394 hw.chipset = ide_palm3710;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100395
Sergei Shtylyova0f403b2008-07-24 22:53:34 +0200396 palm_bk3710_port_info.udma_mask = rate < 100000000 ? ATA_UDMA4 :
397 ATA_UDMA5;
398
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +0200399 rc = ide_host_add(&palm_bk3710_port_info, hws, NULL);
400 if (rc)
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100401 goto out;
402
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100403 return 0;
Bartlomiej Zolnierkiewicz7824bc62008-02-11 00:32:12 +0100404out:
405 printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +0200406 return rc;
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100407}
408
Kay Sievers458622f2008-04-18 13:41:57 -0700409/* work with hotplug and coldplug */
410MODULE_ALIAS("platform:palm_bk3710");
411
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100412static struct platform_driver platform_bk_driver = {
413 .driver = {
414 .name = "palm_bk3710",
Kay Sievers458622f2008-04-18 13:41:57 -0700415 .owner = THIS_MODULE,
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100416 },
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100417};
418
419static int __init palm_bk3710_init(void)
420{
David Brownellbfc2f012008-09-02 20:18:47 +0200421 return platform_driver_probe(&platform_bk_driver, palm_bk3710_probe);
Anton Salnikov7c7e92a2008-02-06 02:57:48 +0100422}
423
424module_init(palm_bk3710_init);
425MODULE_LICENSE("GPL");