]>
Commit | Line | Data |
---|---|---|
7c7e92a9 AS |
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 | ||
ffab6cf4 | 77 | static unsigned ideclk_period; /* in nanoseconds */ |
7c7e92a9 AS |
78 | |
79 | static 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 */ | |
8c6e46dd | 84 | {100, 60}, /* UDMA Mode 4 */ |
7c7e92a9 AS |
85 | }; |
86 | ||
7c7e92a9 AS |
87 | static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev, |
88 | unsigned int mode) | |
89 | { | |
90 | u8 tenv, trp, t0; | |
91 | u32 val32; | |
92 | u16 val16; | |
93 | ||
94 | /* DMA Data Setup */ | |
00fe8b7a | 95 | t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime, |
ffab6cf4 SS |
96 | ideclk_period) - 1; |
97 | tenv = DIV_ROUND_UP(20, ideclk_period) - 1; | |
00fe8b7a | 98 | trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime, |
ffab6cf4 | 99 | ideclk_period) - 1; |
7c7e92a9 AS |
100 | |
101 | /* udmatim Register */ | |
102 | val16 = readw(base + BK3710_UDMATIM) & (dev ? 0xFF0F : 0xFFF0); | |
103 | val16 |= (mode << (dev ? 4 : 0)); | |
104 | writew(val16, base + BK3710_UDMATIM); | |
105 | ||
106 | /* udmastb Ultra DMA Access Strobe Width */ | |
107 | val32 = readl(base + BK3710_UDMASTB) & (0xFF << (dev ? 0 : 8)); | |
108 | val32 |= (t0 << (dev ? 8 : 0)); | |
109 | writel(val32, base + BK3710_UDMASTB); | |
110 | ||
111 | /* udmatrp Ultra DMA Ready to Pause Time */ | |
112 | val32 = readl(base + BK3710_UDMATRP) & (0xFF << (dev ? 0 : 8)); | |
113 | val32 |= (trp << (dev ? 8 : 0)); | |
114 | writel(val32, base + BK3710_UDMATRP); | |
115 | ||
116 | /* udmaenv Ultra DMA envelop Time */ | |
117 | val32 = readl(base + BK3710_UDMAENV) & (0xFF << (dev ? 0 : 8)); | |
118 | val32 |= (tenv << (dev ? 8 : 0)); | |
119 | writel(val32, base + BK3710_UDMAENV); | |
120 | ||
121 | /* Enable UDMA for Device */ | |
122 | val16 = readw(base + BK3710_UDMACTL) | (1 << dev); | |
123 | writew(val16, base + BK3710_UDMACTL); | |
124 | } | |
125 | ||
126 | static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev, | |
127 | unsigned short min_cycle, | |
128 | unsigned int mode) | |
129 | { | |
130 | u8 td, tkw, t0; | |
131 | u32 val32; | |
132 | u16 val16; | |
133 | struct ide_timing *t; | |
134 | int cycletime; | |
135 | ||
136 | t = ide_timing_find_mode(mode); | |
137 | cycletime = max_t(int, t->cycle, min_cycle); | |
138 | ||
139 | /* DMA Data Setup */ | |
ffab6cf4 SS |
140 | t0 = DIV_ROUND_UP(cycletime, ideclk_period); |
141 | td = DIV_ROUND_UP(t->active, ideclk_period); | |
7c7e92a9 AS |
142 | tkw = t0 - td - 1; |
143 | td -= 1; | |
144 | ||
145 | val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8)); | |
146 | val32 |= (td << (dev ? 8 : 0)); | |
147 | writel(val32, base + BK3710_DMASTB); | |
148 | ||
149 | val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8)); | |
150 | val32 |= (tkw << (dev ? 8 : 0)); | |
151 | writel(val32, base + BK3710_DMARCVR); | |
152 | ||
153 | /* Disable UDMA for Device */ | |
154 | val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev); | |
155 | writew(val16, base + BK3710_UDMACTL); | |
156 | } | |
157 | ||
158 | static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate, | |
159 | unsigned int dev, unsigned int cycletime, | |
160 | unsigned int mode) | |
161 | { | |
162 | u8 t2, t2i, t0; | |
163 | u32 val32; | |
164 | struct ide_timing *t; | |
165 | ||
166 | /* PIO Data Setup */ | |
ffab6cf4 | 167 | t0 = DIV_ROUND_UP(cycletime, ideclk_period); |
00fe8b7a | 168 | t2 = DIV_ROUND_UP(ide_timing_find_mode(XFER_PIO_0 + mode)->active, |
ffab6cf4 | 169 | ideclk_period); |
7c7e92a9 AS |
170 | |
171 | t2i = t0 - t2 - 1; | |
172 | t2 -= 1; | |
173 | ||
174 | val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8)); | |
175 | val32 |= (t2 << (dev ? 8 : 0)); | |
176 | writel(val32, base + BK3710_DATSTB); | |
177 | ||
178 | val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8)); | |
179 | val32 |= (t2i << (dev ? 8 : 0)); | |
180 | writel(val32, base + BK3710_DATRCVR); | |
181 | ||
182 | if (mate && mate->present) { | |
183 | u8 mode2 = ide_get_best_pio_mode(mate, 255, 4); | |
184 | ||
185 | if (mode2 < mode) | |
186 | mode = mode2; | |
187 | } | |
188 | ||
189 | /* TASKFILE Setup */ | |
190 | t = ide_timing_find_mode(XFER_PIO_0 + mode); | |
ffab6cf4 SS |
191 | t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period); |
192 | t2 = DIV_ROUND_UP(t->act8b, ideclk_period); | |
7c7e92a9 AS |
193 | |
194 | t2i = t0 - t2 - 1; | |
195 | t2 -= 1; | |
196 | ||
197 | val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8)); | |
198 | val32 |= (t2 << (dev ? 8 : 0)); | |
199 | writel(val32, base + BK3710_REGSTB); | |
200 | ||
201 | val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8)); | |
202 | val32 |= (t2i << (dev ? 8 : 0)); | |
203 | writel(val32, base + BK3710_REGRCVR); | |
204 | } | |
205 | ||
206 | static void palm_bk3710_set_dma_mode(ide_drive_t *drive, u8 xferspeed) | |
207 | { | |
208 | int is_slave = drive->dn & 1; | |
209 | void __iomem *base = (void *)drive->hwif->dma_base; | |
210 | ||
211 | if (xferspeed >= XFER_UDMA_0) { | |
212 | palm_bk3710_setudmamode(base, is_slave, | |
213 | xferspeed - XFER_UDMA_0); | |
214 | } else { | |
215 | palm_bk3710_setdmamode(base, is_slave, drive->id->eide_dma_min, | |
216 | xferspeed); | |
217 | } | |
218 | } | |
219 | ||
220 | static void palm_bk3710_set_pio_mode(ide_drive_t *drive, u8 pio) | |
221 | { | |
222 | unsigned int cycle_time; | |
223 | int is_slave = drive->dn & 1; | |
224 | ide_drive_t *mate; | |
225 | void __iomem *base = (void *)drive->hwif->dma_base; | |
226 | ||
227 | /* | |
228 | * Obtain the drive PIO data for tuning the Palm Chip registers | |
229 | */ | |
230 | cycle_time = ide_pio_cycle_time(drive, pio); | |
231 | mate = ide_get_paired_drive(drive); | |
232 | palm_bk3710_setpiomode(base, mate, is_slave, cycle_time, pio); | |
233 | } | |
234 | ||
235 | static void __devinit palm_bk3710_chipinit(void __iomem *base) | |
236 | { | |
237 | /* | |
238 | * enable the reset_en of ATA controller so that when ata signals | |
239 | * are brought out, by writing into device config. at that | |
240 | * time por_n signal should not be 'Z' and have a stable value. | |
241 | */ | |
242 | writel(0x0300, base + BK3710_MISCCTL); | |
243 | ||
244 | /* wait for some time and deassert the reset of ATA Device. */ | |
245 | mdelay(100); | |
246 | ||
247 | /* Deassert the Reset */ | |
248 | writel(0x0200, base + BK3710_MISCCTL); | |
249 | ||
250 | /* | |
251 | * Program the IDETIMP Register Value based on the following assumptions | |
252 | * | |
253 | * (ATA_IDETIMP_IDEEN , ENABLE ) | | |
254 | * (ATA_IDETIMP_SLVTIMEN , DISABLE) | | |
255 | * (ATA_IDETIMP_RDYSMPL , 70NS) | | |
256 | * (ATA_IDETIMP_RDYRCVRY , 50NS) | | |
257 | * (ATA_IDETIMP_DMAFTIM1 , PIOCOMP) | | |
258 | * (ATA_IDETIMP_PREPOST1 , DISABLE) | | |
259 | * (ATA_IDETIMP_RDYSEN1 , DISABLE) | | |
260 | * (ATA_IDETIMP_PIOFTIM1 , DISABLE) | | |
261 | * (ATA_IDETIMP_DMAFTIM0 , PIOCOMP) | | |
262 | * (ATA_IDETIMP_PREPOST0 , DISABLE) | | |
263 | * (ATA_IDETIMP_RDYSEN0 , DISABLE) | | |
264 | * (ATA_IDETIMP_PIOFTIM0 , DISABLE) | |
265 | */ | |
266 | writew(0xB388, base + BK3710_IDETIMP); | |
267 | ||
268 | /* | |
269 | * Configure SIDETIM Register | |
270 | * (ATA_SIDETIM_RDYSMPS1 ,120NS ) | | |
271 | * (ATA_SIDETIM_RDYRCYS1 ,120NS ) | |
272 | */ | |
273 | writeb(0, base + BK3710_SIDETIM); | |
274 | ||
275 | /* | |
276 | * UDMACTL Ultra-ATA DMA Control | |
277 | * (ATA_UDMACTL_UDMAP1 , 0 ) | | |
278 | * (ATA_UDMACTL_UDMAP0 , 0 ) | |
279 | * | |
280 | */ | |
281 | writew(0, base + BK3710_UDMACTL); | |
282 | ||
283 | /* | |
284 | * MISCCTL Miscellaneous Conrol Register | |
285 | * (ATA_MISCCTL_RSTMODEP , 1) | | |
286 | * (ATA_MISCCTL_RESETP , 0) | | |
287 | * (ATA_MISCCTL_TIMORIDE , 1) | |
288 | */ | |
289 | writel(0x201, base + BK3710_MISCCTL); | |
290 | ||
291 | /* | |
292 | * IORDYTMP IORDY Timer for Primary Register | |
293 | * (ATA_IORDYTMP_IORDYTMP , 0xffff ) | |
294 | */ | |
295 | writel(0xFFFF, base + BK3710_IORDYTMP); | |
296 | ||
297 | /* | |
298 | * Configure BMISP Register | |
299 | * (ATA_BMISP_DMAEN1 , DISABLE ) | | |
300 | * (ATA_BMISP_DMAEN0 , DISABLE ) | | |
301 | * (ATA_BMISP_IORDYINT , CLEAR) | | |
302 | * (ATA_BMISP_INTRSTAT , CLEAR) | | |
303 | * (ATA_BMISP_DMAERROR , CLEAR) | |
304 | */ | |
305 | writew(0, base + BK3710_BMISP); | |
306 | ||
307 | palm_bk3710_setpiomode(base, NULL, 0, 600, 0); | |
308 | palm_bk3710_setpiomode(base, NULL, 1, 600, 0); | |
309 | } | |
c79b60dd BZ |
310 | |
311 | static u8 __devinit palm_bk3710_cable_detect(ide_hwif_t *hwif) | |
312 | { | |
313 | return ATA_CBL_PATA80; | |
314 | } | |
315 | ||
b552a2c1 BZ |
316 | static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif, |
317 | const struct ide_port_info *d) | |
318 | { | |
b552a2c1 BZ |
319 | printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name); |
320 | ||
321 | if (ide_allocate_dma_engine(hwif)) | |
322 | return -1; | |
323 | ||
81e8d5a3 BZ |
324 | hwif->dma_base = hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET; |
325 | ||
326 | hwif->dma_ops = &sff_dma_ops; | |
b552a2c1 BZ |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
ac95beed BZ |
331 | static const struct ide_port_ops palm_bk3710_ports_ops = { |
332 | .set_pio_mode = palm_bk3710_set_pio_mode, | |
333 | .set_dma_mode = palm_bk3710_set_dma_mode, | |
334 | .cable_detect = palm_bk3710_cable_detect, | |
335 | }; | |
c79b60dd BZ |
336 | |
337 | static const struct ide_port_info __devinitdata palm_bk3710_port_info = { | |
b552a2c1 | 338 | .init_dma = palm_bk3710_init_dma, |
ac95beed | 339 | .port_ops = &palm_bk3710_ports_ops, |
c5dd43ec | 340 | .host_flags = IDE_HFLAG_MMIO, |
c79b60dd BZ |
341 | .pio_mask = ATA_PIO4, |
342 | .udma_mask = ATA_UDMA4, /* (input clk 99MHz) */ | |
343 | .mwdma_mask = ATA_MWDMA2, | |
344 | }; | |
345 | ||
7c7e92a9 AS |
346 | static int __devinit palm_bk3710_probe(struct platform_device *pdev) |
347 | { | |
ffab6cf4 | 348 | struct clk *clk; |
7c7e92a9 | 349 | struct resource *mem, *irq; |
48c3c107 | 350 | struct ide_host *host; |
ffab6cf4 | 351 | unsigned long base, rate; |
ce42a549 | 352 | int i; |
c97c6aca | 353 | hw_regs_t hw, *hws[] = { &hw, NULL, NULL, NULL }; |
7c7e92a9 | 354 | |
ffab6cf4 SS |
355 | clk = clk_get(NULL, "IDECLK"); |
356 | if (IS_ERR(clk)) | |
7c7e92a9 AS |
357 | return -ENODEV; |
358 | ||
ffab6cf4 SS |
359 | clk_enable(clk); |
360 | rate = clk_get_rate(clk); | |
361 | ideclk_period = 1000000000UL / rate; | |
362 | ||
7c7e92a9 | 363 | /* Register the IDE interface with Linux ATA Interface */ |
7824bc6b | 364 | memset(&hw, 0, sizeof(hw)); |
7c7e92a9 AS |
365 | |
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 | } | |
ce42a549 | 371 | |
7c7e92a9 AS |
372 | 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 | ||
ce42a549 SS |
378 | 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); | |
7c7e92a9 AS |
385 | |
386 | /* Configure the Palm Chip controller */ | |
ce42a549 | 387 | palm_bk3710_chipinit((void __iomem *)base); |
7c7e92a9 | 388 | |
7824bc6b | 389 | for (i = 0; i < IDE_NR_PORTS - 2; i++) |
ce42a549 SS |
390 | 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; | |
7824bc6b BZ |
392 | hw.irq = irq->start; |
393 | hw.chipset = ide_palm3710; | |
7c7e92a9 | 394 | |
48c3c107 BZ |
395 | host = ide_host_alloc(&palm_bk3710_port_info, hws); |
396 | if (host == NULL) | |
7824bc6b BZ |
397 | goto out; |
398 | ||
48c3c107 | 399 | ide_host_register(host, &palm_bk3710_port_info, hws); |
c92a7f1d | 400 | |
7c7e92a9 | 401 | return 0; |
7824bc6b BZ |
402 | out: |
403 | printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n"); | |
404 | return -ENODEV; | |
7c7e92a9 AS |
405 | } |
406 | ||
458622fc KS |
407 | /* work with hotplug and coldplug */ |
408 | MODULE_ALIAS("platform:palm_bk3710"); | |
409 | ||
7c7e92a9 AS |
410 | static struct platform_driver platform_bk_driver = { |
411 | .driver = { | |
412 | .name = "palm_bk3710", | |
458622fc | 413 | .owner = THIS_MODULE, |
7c7e92a9 AS |
414 | }, |
415 | .probe = palm_bk3710_probe, | |
416 | .remove = NULL, | |
417 | }; | |
418 | ||
419 | static int __init palm_bk3710_init(void) | |
420 | { | |
421 | return platform_driver_register(&platform_bk_driver); | |
422 | } | |
423 | ||
424 | module_init(palm_bk3710_init); | |
425 | MODULE_LICENSE("GPL"); |