blob: da6383a934ac33ac57a39a010f23f94ba1c9daa1 [file] [log] [blame]
Ian Molton1f192012008-07-15 15:09:43 +01001/*
2 *
3 * Toshiba T7L66XB core mfd support
4 *
5 * Copyright (c) 2005, 2007, 2008 Ian Molton
6 * Copyright (c) 2008 Dmitry Baryshkov
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * T7L66 features:
13 *
14 * Supported in this driver:
15 * SD/MMC
16 * SM/NAND flash controller
17 *
18 * As yet not supported
19 * GPIO interface (on NAND pins)
20 * Serial interface
21 * TFT 'interface converter'
22 * PCMCIA interface logic
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
Ian Molton7acb7062008-10-09 20:06:09 +020027#include <linux/err.h>
Ian Molton1f192012008-07-15 15:09:43 +010028#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ian Molton1f192012008-07-15 15:09:43 +010030#include <linux/irq.h>
Ian Molton7acb7062008-10-09 20:06:09 +020031#include <linux/clk.h>
Ian Molton1f192012008-07-15 15:09:43 +010032#include <linux/platform_device.h>
33#include <linux/mfd/core.h>
34#include <linux/mfd/tmio.h>
35#include <linux/mfd/t7l66xb.h>
36
37enum {
38 T7L66XB_CELL_NAND,
39 T7L66XB_CELL_MMC,
40};
41
Ian Molton64e88672010-01-06 13:51:48 +010042static const struct resource t7l66xb_mmc_resources[] = {
43 {
44 .start = 0x800,
45 .end = 0x9ff,
46 .flags = IORESOURCE_MEM,
47 },
48 {
49 .start = IRQ_T7L66XB_MMC,
50 .end = IRQ_T7L66XB_MMC,
51 .flags = IORESOURCE_IRQ,
52 },
53};
54
Ian Molton1f192012008-07-15 15:09:43 +010055#define SCR_REVID 0x08 /* b Revision ID */
56#define SCR_IMR 0x42 /* b Interrupt Mask */
57#define SCR_DEV_CTL 0xe0 /* b Device control */
58#define SCR_ISR 0xe1 /* b Interrupt Status */
59#define SCR_GPO_OC 0xf0 /* b GPO output control */
60#define SCR_GPO_OS 0xf1 /* b GPO output enable */
61#define SCR_GPI_S 0xf2 /* w GPI status */
62#define SCR_APDC 0xf8 /* b Active pullup down ctrl */
63
64#define SCR_DEV_CTL_USB BIT(0) /* USB enable */
65#define SCR_DEV_CTL_MMC BIT(1) /* MMC enable */
66
67/*--------------------------------------------------------------------------*/
68
69struct t7l66xb {
70 void __iomem *scr;
71 /* Lock to protect registers requiring read/modify/write ops. */
72 spinlock_t lock;
73
74 struct resource rscr;
Ian Molton7acb7062008-10-09 20:06:09 +020075 struct clk *clk48m;
76 struct clk *clk32k;
Ian Molton1f192012008-07-15 15:09:43 +010077 int irq;
78 int irq_base;
79};
80
81/*--------------------------------------------------------------------------*/
82
83static int t7l66xb_mmc_enable(struct platform_device *mmc)
84{
85 struct platform_device *dev = to_platform_device(mmc->dev.parent);
Ian Molton1f192012008-07-15 15:09:43 +010086 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
87 unsigned long flags;
88 u8 dev_ctl;
89
Ian Molton7acb7062008-10-09 20:06:09 +020090 clk_enable(t7l66xb->clk32k);
Ian Molton1f192012008-07-15 15:09:43 +010091
92 spin_lock_irqsave(&t7l66xb->lock, flags);
93
94 dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
95 dev_ctl |= SCR_DEV_CTL_MMC;
96 tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
97
98 spin_unlock_irqrestore(&t7l66xb->lock, flags);
99
Ian Molton64e88672010-01-06 13:51:48 +0100100 tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
101 t7l66xb_mmc_resources[0].start & 0xfffe);
102
Ian Molton1f192012008-07-15 15:09:43 +0100103 return 0;
104}
105
106static int t7l66xb_mmc_disable(struct platform_device *mmc)
107{
108 struct platform_device *dev = to_platform_device(mmc->dev.parent);
Ian Molton1f192012008-07-15 15:09:43 +0100109 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
110 unsigned long flags;
111 u8 dev_ctl;
112
113 spin_lock_irqsave(&t7l66xb->lock, flags);
114
115 dev_ctl = tmio_ioread8(t7l66xb->scr + SCR_DEV_CTL);
116 dev_ctl &= ~SCR_DEV_CTL_MMC;
117 tmio_iowrite8(dev_ctl, t7l66xb->scr + SCR_DEV_CTL);
118
119 spin_unlock_irqrestore(&t7l66xb->lock, flags);
120
Ian Molton7acb7062008-10-09 20:06:09 +0200121 clk_disable(t7l66xb->clk32k);
Ian Molton1f192012008-07-15 15:09:43 +0100122
123 return 0;
124}
125
Ian Molton64e88672010-01-06 13:51:48 +0100126static void t7l66xb_mmc_pwr(struct platform_device *mmc, int state)
127{
128 struct platform_device *dev = to_platform_device(mmc->dev.parent);
129 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
130
131 tmio_core_mmc_pwr(t7l66xb->scr + 0x200, 0, state);
132}
133
134static void t7l66xb_mmc_clk_div(struct platform_device *mmc, int state)
135{
136 struct platform_device *dev = to_platform_device(mmc->dev.parent);
137 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
138
139 tmio_core_mmc_clk_div(t7l66xb->scr + 0x200, 0, state);
140}
141
Ian Molton1f192012008-07-15 15:09:43 +0100142/*--------------------------------------------------------------------------*/
143
Samuel Ortiz4d3792e2009-06-15 15:43:31 +0200144static struct tmio_mmc_data t7166xb_mmc_data = {
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200145 .hclk = 24000000,
Ian Molton64e88672010-01-06 13:51:48 +0100146 .set_pwr = t7l66xb_mmc_pwr,
147 .set_clk_div = t7l66xb_mmc_clk_div,
Ian Molton1f192012008-07-15 15:09:43 +0100148};
149
Tobias Klauser3446d4b2009-02-17 10:11:42 +0100150static const struct resource t7l66xb_nand_resources[] = {
Ian Molton1f192012008-07-15 15:09:43 +0100151 {
152 .start = 0xc00,
153 .end = 0xc07,
154 .flags = IORESOURCE_MEM,
155 },
156 {
157 .start = 0x0100,
158 .end = 0x01ff,
159 .flags = IORESOURCE_MEM,
160 },
161 {
162 .start = IRQ_T7L66XB_NAND,
163 .end = IRQ_T7L66XB_NAND,
164 .flags = IORESOURCE_IRQ,
165 },
166};
167
168static struct mfd_cell t7l66xb_cells[] = {
169 [T7L66XB_CELL_MMC] = {
170 .name = "tmio-mmc",
171 .enable = t7l66xb_mmc_enable,
172 .disable = t7l66xb_mmc_disable,
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200173 .driver_data = &t7166xb_mmc_data,
Ian Molton1f192012008-07-15 15:09:43 +0100174 .num_resources = ARRAY_SIZE(t7l66xb_mmc_resources),
175 .resources = t7l66xb_mmc_resources,
176 },
177 [T7L66XB_CELL_NAND] = {
178 .name = "tmio-nand",
179 .num_resources = ARRAY_SIZE(t7l66xb_nand_resources),
180 .resources = t7l66xb_nand_resources,
181 },
182};
183
184/*--------------------------------------------------------------------------*/
185
186/* Handle the T7L66XB interrupt mux */
187static void t7l66xb_irq(unsigned int irq, struct irq_desc *desc)
188{
189 struct t7l66xb *t7l66xb = get_irq_data(irq);
190 unsigned int isr;
191 unsigned int i, irq_base;
192
193 irq_base = t7l66xb->irq_base;
194
195 while ((isr = tmio_ioread8(t7l66xb->scr + SCR_ISR) &
196 ~tmio_ioread8(t7l66xb->scr + SCR_IMR)))
197 for (i = 0; i < T7L66XB_NR_IRQS; i++)
198 if (isr & (1 << i))
199 generic_handle_irq(irq_base + i);
200}
201
202static void t7l66xb_irq_mask(unsigned int irq)
203{
204 struct t7l66xb *t7l66xb = get_irq_chip_data(irq);
205 unsigned long flags;
206 u8 imr;
207
208 spin_lock_irqsave(&t7l66xb->lock, flags);
209 imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
210 imr |= 1 << (irq - t7l66xb->irq_base);
211 tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
212 spin_unlock_irqrestore(&t7l66xb->lock, flags);
213}
214
215static void t7l66xb_irq_unmask(unsigned int irq)
216{
217 struct t7l66xb *t7l66xb = get_irq_chip_data(irq);
218 unsigned long flags;
219 u8 imr;
220
221 spin_lock_irqsave(&t7l66xb->lock, flags);
222 imr = tmio_ioread8(t7l66xb->scr + SCR_IMR);
223 imr &= ~(1 << (irq - t7l66xb->irq_base));
224 tmio_iowrite8(imr, t7l66xb->scr + SCR_IMR);
225 spin_unlock_irqrestore(&t7l66xb->lock, flags);
226}
227
228static struct irq_chip t7l66xb_chip = {
229 .name = "t7l66xb",
230 .ack = t7l66xb_irq_mask,
231 .mask = t7l66xb_irq_mask,
232 .unmask = t7l66xb_irq_unmask,
233};
234
235/*--------------------------------------------------------------------------*/
236
237/* Install the IRQ handler */
238static void t7l66xb_attach_irq(struct platform_device *dev)
239{
240 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
241 unsigned int irq, irq_base;
242
243 irq_base = t7l66xb->irq_base;
244
245 for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
246 set_irq_chip(irq, &t7l66xb_chip);
247 set_irq_chip_data(irq, t7l66xb);
248 set_irq_handler(irq, handle_level_irq);
249#ifdef CONFIG_ARM
250 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
251#endif
252 }
253
254 set_irq_type(t7l66xb->irq, IRQ_TYPE_EDGE_FALLING);
255 set_irq_data(t7l66xb->irq, t7l66xb);
256 set_irq_chained_handler(t7l66xb->irq, t7l66xb_irq);
257}
258
259static void t7l66xb_detach_irq(struct platform_device *dev)
260{
261 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
262 unsigned int irq, irq_base;
263
264 irq_base = t7l66xb->irq_base;
265
266 set_irq_chained_handler(t7l66xb->irq, NULL);
267 set_irq_data(t7l66xb->irq, NULL);
268
269 for (irq = irq_base; irq < irq_base + T7L66XB_NR_IRQS; irq++) {
270#ifdef CONFIG_ARM
271 set_irq_flags(irq, 0);
272#endif
273 set_irq_chip(irq, NULL);
274 set_irq_chip_data(irq, NULL);
275 }
276}
277
278/*--------------------------------------------------------------------------*/
279
280#ifdef CONFIG_PM
281static int t7l66xb_suspend(struct platform_device *dev, pm_message_t state)
282{
Ian Molton7acb7062008-10-09 20:06:09 +0200283 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
Ian Molton1f192012008-07-15 15:09:43 +0100284 struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
285
286 if (pdata && pdata->suspend)
287 pdata->suspend(dev);
Ian Molton7acb7062008-10-09 20:06:09 +0200288 clk_disable(t7l66xb->clk48m);
Ian Molton1f192012008-07-15 15:09:43 +0100289
290 return 0;
291}
292
293static int t7l66xb_resume(struct platform_device *dev)
294{
Ian Molton7acb7062008-10-09 20:06:09 +0200295 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
Ian Molton1f192012008-07-15 15:09:43 +0100296 struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
297
Ian Molton7acb7062008-10-09 20:06:09 +0200298 clk_enable(t7l66xb->clk48m);
Ian Molton1f192012008-07-15 15:09:43 +0100299 if (pdata && pdata->resume)
300 pdata->resume(dev);
301
Ian Molton64e88672010-01-06 13:51:48 +0100302 tmio_core_mmc_enable(t7l66xb->scr + 0x200, 0,
303 t7l66xb_mmc_resources[0].start & 0xfffe);
304
Ian Molton1f192012008-07-15 15:09:43 +0100305 return 0;
306}
307#else
308#define t7l66xb_suspend NULL
309#define t7l66xb_resume NULL
310#endif
311
312/*--------------------------------------------------------------------------*/
313
314static int t7l66xb_probe(struct platform_device *dev)
315{
316 struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
317 struct t7l66xb *t7l66xb;
318 struct resource *iomem, *rscr;
319 int ret;
320
321 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
322 if (!iomem)
323 return -EINVAL;
324
325 t7l66xb = kzalloc(sizeof *t7l66xb, GFP_KERNEL);
326 if (!t7l66xb)
327 return -ENOMEM;
328
329 spin_lock_init(&t7l66xb->lock);
330
331 platform_set_drvdata(dev, t7l66xb);
332
333 ret = platform_get_irq(dev, 0);
334 if (ret >= 0)
335 t7l66xb->irq = ret;
336 else
337 goto err_noirq;
338
339 t7l66xb->irq_base = pdata->irq_base;
340
Ian Molton7acb7062008-10-09 20:06:09 +0200341 t7l66xb->clk32k = clk_get(&dev->dev, "CLK_CK32K");
342 if (IS_ERR(t7l66xb->clk32k)) {
343 ret = PTR_ERR(t7l66xb->clk32k);
344 goto err_clk32k_get;
345 }
346
347 t7l66xb->clk48m = clk_get(&dev->dev, "CLK_CK48M");
348 if (IS_ERR(t7l66xb->clk48m)) {
349 ret = PTR_ERR(t7l66xb->clk48m);
350 clk_put(t7l66xb->clk32k);
351 goto err_clk48m_get;
352 }
353
Ian Molton1f192012008-07-15 15:09:43 +0100354 rscr = &t7l66xb->rscr;
355 rscr->name = "t7l66xb-core";
356 rscr->start = iomem->start;
357 rscr->end = iomem->start + 0xff;
358 rscr->flags = IORESOURCE_MEM;
359
360 ret = request_resource(iomem, rscr);
361 if (ret)
362 goto err_request_scr;
363
H Hartley Sweetenc02e6a52010-01-05 19:58:34 +0100364 t7l66xb->scr = ioremap(rscr->start, resource_size(rscr));
Ian Molton1f192012008-07-15 15:09:43 +0100365 if (!t7l66xb->scr) {
366 ret = -ENOMEM;
367 goto err_ioremap;
368 }
369
Ian Molton7acb7062008-10-09 20:06:09 +0200370 clk_enable(t7l66xb->clk48m);
371
Ian Molton1f192012008-07-15 15:09:43 +0100372 if (pdata && pdata->enable)
373 pdata->enable(dev);
374
375 /* Mask all interrupts */
376 tmio_iowrite8(0xbf, t7l66xb->scr + SCR_IMR);
377
378 printk(KERN_INFO "%s rev %d @ 0x%08lx, irq %d\n",
379 dev->name, tmio_ioread8(t7l66xb->scr + SCR_REVID),
380 (unsigned long)iomem->start, t7l66xb->irq);
381
382 t7l66xb_attach_irq(dev);
383
384 t7l66xb_cells[T7L66XB_CELL_NAND].driver_data = pdata->nand_data;
Samuel Ortiz56bf2bd2008-08-01 00:16:13 +0200385 t7l66xb_cells[T7L66XB_CELL_NAND].platform_data =
386 &t7l66xb_cells[T7L66XB_CELL_NAND];
387 t7l66xb_cells[T7L66XB_CELL_NAND].data_size =
388 sizeof(t7l66xb_cells[T7L66XB_CELL_NAND]);
Ian Molton1f192012008-07-15 15:09:43 +0100389
Ian Molton8a4fbe02008-08-04 18:06:18 +0200390 t7l66xb_cells[T7L66XB_CELL_MMC].platform_data =
391 &t7l66xb_cells[T7L66XB_CELL_MMC];
392 t7l66xb_cells[T7L66XB_CELL_MMC].data_size =
393 sizeof(t7l66xb_cells[T7L66XB_CELL_MMC]);
394
Samuel Ortiz56bf2bd2008-08-01 00:16:13 +0200395 ret = mfd_add_devices(&dev->dev, dev->id,
396 t7l66xb_cells, ARRAY_SIZE(t7l66xb_cells),
397 iomem, t7l66xb->irq_base);
Ian Molton1f192012008-07-15 15:09:43 +0100398
399 if (!ret)
400 return 0;
401
402 t7l66xb_detach_irq(dev);
403 iounmap(t7l66xb->scr);
404err_ioremap:
405 release_resource(&t7l66xb->rscr);
Ian Molton1f192012008-07-15 15:09:43 +0100406err_request_scr:
Ian Molton7acb7062008-10-09 20:06:09 +0200407 clk_put(t7l66xb->clk48m);
408err_clk48m_get:
409 clk_put(t7l66xb->clk32k);
410err_clk32k_get:
411err_noirq:
Julia Lawall0e820ab2009-12-22 21:31:43 +0100412 kfree(t7l66xb);
Ian Molton1f192012008-07-15 15:09:43 +0100413 return ret;
414}
415
416static int t7l66xb_remove(struct platform_device *dev)
417{
418 struct t7l66xb_platform_data *pdata = dev->dev.platform_data;
419 struct t7l66xb *t7l66xb = platform_get_drvdata(dev);
420 int ret;
421
422 ret = pdata->disable(dev);
Ian Molton7acb7062008-10-09 20:06:09 +0200423 clk_disable(t7l66xb->clk48m);
424 clk_put(t7l66xb->clk48m);
Ian Molton1f192012008-07-15 15:09:43 +0100425 t7l66xb_detach_irq(dev);
426 iounmap(t7l66xb->scr);
427 release_resource(&t7l66xb->rscr);
Samuel Ortiz56bf2bd2008-08-01 00:16:13 +0200428 mfd_remove_devices(&dev->dev);
Ian Molton1f192012008-07-15 15:09:43 +0100429 platform_set_drvdata(dev, NULL);
430 kfree(t7l66xb);
431
432 return ret;
433
434}
435
436static struct platform_driver t7l66xb_platform_driver = {
437 .driver = {
438 .name = "t7l66xb",
439 .owner = THIS_MODULE,
440 },
441 .suspend = t7l66xb_suspend,
442 .resume = t7l66xb_resume,
443 .probe = t7l66xb_probe,
444 .remove = t7l66xb_remove,
445};
446
447/*--------------------------------------------------------------------------*/
448
449static int __init t7l66xb_init(void)
450{
451 int retval = 0;
452
453 retval = platform_driver_register(&t7l66xb_platform_driver);
454 return retval;
455}
456
457static void __exit t7l66xb_exit(void)
458{
459 platform_driver_unregister(&t7l66xb_platform_driver);
460}
461
462module_init(t7l66xb_init);
463module_exit(t7l66xb_exit);
464
465MODULE_DESCRIPTION("Toshiba T7L66XB core driver");
466MODULE_LICENSE("GPL v2");
467MODULE_AUTHOR("Ian Molton");
468MODULE_ALIAS("platform:t7l66xb");