Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Mobile SDHI |
| 3 | * |
| 4 | * Copyright (C) 2009 Magnus Damm |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Based on "Compaq ASIC3 support": |
| 11 | * |
| 12 | * Copyright 2001 Compaq Computer Corporation. |
| 13 | * Copyright 2004-2005 Phil Blundell |
| 14 | * Copyright 2007-2008 OpenedHand Ltd. |
| 15 | * |
| 16 | * Authors: Phil Blundell <pb@handhelds.org>, |
| 17 | * Samuel Ortiz <sameo@openedhand.com> |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Magnus Damm | 3c49e81 | 2010-02-22 13:37:09 +0900 | [diff] [blame] | 25 | #include <linux/mmc/host.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 26 | #include <linux/mfd/core.h> |
| 27 | #include <linux/mfd/tmio.h> |
Magnus Damm | be9cd7b | 2009-11-27 04:31:27 +0000 | [diff] [blame] | 28 | #include <linux/mfd/sh_mobile_sdhi.h> |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 29 | #include <linux/sh_dma.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 30 | |
| 31 | struct sh_mobile_sdhi { |
| 32 | struct clk *clk; |
| 33 | struct tmio_mmc_data mmc_data; |
| 34 | struct mfd_cell cell_mmc; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 35 | struct sh_dmae_slave param_tx; |
| 36 | struct sh_dmae_slave param_rx; |
| 37 | struct tmio_mmc_dma dma_priv; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static struct resource sh_mobile_sdhi_resources[] = { |
| 41 | { |
| 42 | .start = 0x000, |
| 43 | .end = 0x1ff, |
| 44 | .flags = IORESOURCE_MEM, |
| 45 | }, |
| 46 | { |
| 47 | .start = 0, |
| 48 | .end = 0, |
| 49 | .flags = IORESOURCE_IRQ, |
| 50 | }, |
| 51 | }; |
| 52 | |
| 53 | static struct mfd_cell sh_mobile_sdhi_cell = { |
| 54 | .name = "tmio-mmc", |
| 55 | .num_resources = ARRAY_SIZE(sh_mobile_sdhi_resources), |
| 56 | .resources = sh_mobile_sdhi_resources, |
| 57 | }; |
| 58 | |
Magnus Damm | be9cd7b | 2009-11-27 04:31:27 +0000 | [diff] [blame] | 59 | static void sh_mobile_sdhi_set_pwr(struct platform_device *tmio, int state) |
| 60 | { |
| 61 | struct platform_device *pdev = to_platform_device(tmio->dev.parent); |
| 62 | struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; |
| 63 | |
| 64 | if (p && p->set_pwr) |
| 65 | p->set_pwr(pdev, state); |
| 66 | } |
| 67 | |
Arnd Hannemann | 998283e | 2010-08-24 17:27:00 +0200 | [diff] [blame] | 68 | static int sh_mobile_sdhi_get_cd(struct platform_device *tmio) |
| 69 | { |
| 70 | struct platform_device *pdev = to_platform_device(tmio->dev.parent); |
| 71 | struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; |
| 72 | |
| 73 | if (p && p->get_cd) |
| 74 | return p->get_cd(pdev); |
| 75 | else |
| 76 | return -ENOSYS; |
| 77 | } |
| 78 | |
Paul Mundt | 25ab998 | 2010-09-16 17:14:40 +0900 | [diff] [blame] | 79 | static int __devinit sh_mobile_sdhi_probe(struct platform_device *pdev) |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 80 | { |
| 81 | struct sh_mobile_sdhi *priv; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 82 | struct tmio_mmc_data *mmc_data; |
| 83 | struct sh_mobile_sdhi_info *p = pdev->dev.platform_data; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 84 | struct resource *mem; |
| 85 | char clk_name[8]; |
| 86 | int ret, irq; |
| 87 | |
| 88 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 89 | if (!mem) |
| 90 | dev_err(&pdev->dev, "missing MEM resource\n"); |
| 91 | |
| 92 | irq = platform_get_irq(pdev, 0); |
| 93 | if (irq < 0) |
| 94 | dev_err(&pdev->dev, "missing IRQ resource\n"); |
| 95 | |
| 96 | if (!mem || (irq < 0)) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL); |
| 100 | if (priv == NULL) { |
| 101 | dev_err(&pdev->dev, "kzalloc failed\n"); |
| 102 | return -ENOMEM; |
| 103 | } |
| 104 | |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 105 | mmc_data = &priv->mmc_data; |
| 106 | |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 107 | snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id); |
| 108 | priv->clk = clk_get(&pdev->dev, clk_name); |
| 109 | if (IS_ERR(priv->clk)) { |
| 110 | dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name); |
| 111 | ret = PTR_ERR(priv->clk); |
| 112 | kfree(priv); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | clk_enable(priv->clk); |
| 117 | |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 118 | mmc_data->hclk = clk_get_rate(priv->clk); |
| 119 | mmc_data->set_pwr = sh_mobile_sdhi_set_pwr; |
Arnd Hannemann | 998283e | 2010-08-24 17:27:00 +0200 | [diff] [blame] | 120 | mmc_data->get_cd = sh_mobile_sdhi_get_cd; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 121 | mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED; |
Guennadi Liakhovetski | bb0fe53 | 2010-05-19 18:37:36 +0000 | [diff] [blame] | 122 | if (p) { |
Guennadi Liakhovetski | f87c20a | 2010-05-19 18:36:08 +0000 | [diff] [blame] | 123 | mmc_data->flags = p->tmio_flags; |
Guennadi Liakhovetski | bb0fe53 | 2010-05-19 18:37:36 +0000 | [diff] [blame] | 124 | mmc_data->ocr_mask = p->tmio_ocr_mask; |
Arnd Hannemann | 998283e | 2010-08-24 17:27:00 +0200 | [diff] [blame] | 125 | mmc_data->capabilities |= p->tmio_caps; |
Guennadi Liakhovetski | bb0fe53 | 2010-05-19 18:37:36 +0000 | [diff] [blame] | 126 | } |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 127 | |
Yusuke Goda | f1334fb | 2010-08-30 11:50:19 +0100 | [diff] [blame] | 128 | /* |
| 129 | * All SDHI blocks support 2-byte and larger block sizes in 4-bit |
| 130 | * bus width mode. |
| 131 | */ |
| 132 | mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES; |
| 133 | |
Arnd Hannemann | 23b6607 | 2010-12-28 23:22:32 +0100 | [diff] [blame] | 134 | /* |
| 135 | * All SDHI blocks support SDIO IRQ signalling. |
| 136 | */ |
| 137 | mmc_data->flags |= TMIO_MMC_SDIO_IRQ; |
| 138 | |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 139 | if (p && p->dma_slave_tx >= 0 && p->dma_slave_rx >= 0) { |
| 140 | priv->param_tx.slave_id = p->dma_slave_tx; |
| 141 | priv->param_rx.slave_id = p->dma_slave_rx; |
| 142 | priv->dma_priv.chan_priv_tx = &priv->param_tx; |
| 143 | priv->dma_priv.chan_priv_rx = &priv->param_rx; |
Guennadi Liakhovetski | 39a65a0 | 2010-11-23 17:24:19 +0100 | [diff] [blame] | 144 | priv->dma_priv.alignment_shift = 1; /* 2-byte alignment */ |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 145 | mmc_data->dma = &priv->dma_priv; |
| 146 | } |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 147 | |
| 148 | memcpy(&priv->cell_mmc, &sh_mobile_sdhi_cell, sizeof(priv->cell_mmc)); |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 149 | priv->cell_mmc.driver_data = mmc_data; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 150 | priv->cell_mmc.platform_data = &priv->cell_mmc; |
| 151 | priv->cell_mmc.data_size = sizeof(priv->cell_mmc); |
| 152 | |
| 153 | platform_set_drvdata(pdev, priv); |
| 154 | |
| 155 | ret = mfd_add_devices(&pdev->dev, pdev->id, |
| 156 | &priv->cell_mmc, 1, mem, irq); |
| 157 | if (ret) { |
| 158 | clk_disable(priv->clk); |
| 159 | clk_put(priv->clk); |
| 160 | kfree(priv); |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int sh_mobile_sdhi_remove(struct platform_device *pdev) |
| 167 | { |
| 168 | struct sh_mobile_sdhi *priv = platform_get_drvdata(pdev); |
| 169 | |
| 170 | mfd_remove_devices(&pdev->dev); |
| 171 | clk_disable(priv->clk); |
| 172 | clk_put(priv->clk); |
| 173 | kfree(priv); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static struct platform_driver sh_mobile_sdhi_driver = { |
| 179 | .driver = { |
| 180 | .name = "sh_mobile_sdhi", |
| 181 | .owner = THIS_MODULE, |
| 182 | }, |
| 183 | .probe = sh_mobile_sdhi_probe, |
| 184 | .remove = __devexit_p(sh_mobile_sdhi_remove), |
| 185 | }; |
| 186 | |
| 187 | static int __init sh_mobile_sdhi_init(void) |
| 188 | { |
| 189 | return platform_driver_register(&sh_mobile_sdhi_driver); |
| 190 | } |
| 191 | |
| 192 | static void __exit sh_mobile_sdhi_exit(void) |
| 193 | { |
| 194 | platform_driver_unregister(&sh_mobile_sdhi_driver); |
| 195 | } |
| 196 | |
| 197 | module_init(sh_mobile_sdhi_init); |
| 198 | module_exit(sh_mobile_sdhi_exit); |
| 199 | |
| 200 | MODULE_DESCRIPTION("SuperH Mobile SDHI driver"); |
| 201 | MODULE_AUTHOR("Magnus Damm"); |
| 202 | MODULE_LICENSE("GPL v2"); |