Wolfram Sang | afa17a5 | 2009-11-13 06:14:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CAN bus driver for the Freescale MPC5xxx embedded CPU. |
| 3 | * |
| 4 | * Copyright (C) 2004-2005 Andrey Volkov <avolkov@varma-el.com>, |
| 5 | * Varma Electronics Oy |
| 6 | * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com> |
| 7 | * Copyright (C) 2009 Wolfram Sang, Pengutronix <w.sang@pengutronix.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the version 2 of the GNU General Public License |
| 11 | * as published by the Free Software Foundation |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/can.h> |
| 29 | #include <linux/can/dev.h> |
| 30 | #include <linux/of_platform.h> |
| 31 | #include <sysdev/fsl_soc.h> |
| 32 | #include <linux/io.h> |
| 33 | #include <asm/mpc52xx.h> |
| 34 | |
| 35 | #include "mscan.h" |
| 36 | |
Wolfram Sang | afa17a5 | 2009-11-13 06:14:52 +0000 | [diff] [blame] | 37 | #define DRV_NAME "mpc5xxx_can" |
| 38 | |
| 39 | static struct of_device_id mpc52xx_cdm_ids[] __devinitdata = { |
| 40 | { .compatible = "fsl,mpc5200-cdm", }, |
| 41 | { .compatible = "fsl,mpc5200b-cdm", }, |
| 42 | {} |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Get the frequency of the external oscillator clock connected |
| 47 | * to the SYS_XTAL_IN pin, or return 0 if it cannot be determined. |
| 48 | */ |
| 49 | static unsigned int __devinit mpc52xx_can_xtal_freq(struct of_device *of) |
| 50 | { |
| 51 | struct mpc52xx_cdm __iomem *cdm; |
| 52 | struct device_node *np_cdm; |
| 53 | unsigned int freq; |
| 54 | u32 val; |
| 55 | |
| 56 | freq = mpc5xxx_get_bus_frequency(of->node); |
| 57 | if (!freq) |
| 58 | return 0; |
| 59 | |
| 60 | /* |
| 61 | * Determine SYS_XTAL_IN frequency from the clock domain settings |
| 62 | */ |
| 63 | np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids); |
| 64 | if (!np_cdm) { |
| 65 | dev_err(&of->dev, "can't get clock node!\n"); |
| 66 | return 0; |
| 67 | } |
| 68 | cdm = of_iomap(np_cdm, 0); |
| 69 | of_node_put(np_cdm); |
| 70 | |
| 71 | if (in_8(&cdm->ipb_clk_sel) & 0x1) |
| 72 | freq *= 2; |
Wolfram Sang | 0285e7c | 2009-11-16 12:57:45 +0000 | [diff] [blame] | 73 | val = in_be32(&cdm->rstcfg); |
| 74 | |
| 75 | freq *= (val & (1 << 5)) ? 8 : 4; |
| 76 | freq /= (val & (1 << 6)) ? 12 : 16; |
Wolfram Sang | afa17a5 | 2009-11-13 06:14:52 +0000 | [diff] [blame] | 77 | |
| 78 | iounmap(cdm); |
| 79 | |
| 80 | return freq; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Get frequency of the MSCAN clock source |
| 85 | * |
| 86 | * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock (IP_CLK) |
| 87 | * can be selected. According to the MPC5200 user's manual, the oscillator |
| 88 | * clock is the better choice as it has less jitter but due to a hardware |
| 89 | * bug, it can not be selected for the old MPC5200 Rev. A chips. |
| 90 | */ |
| 91 | |
| 92 | static unsigned int __devinit mpc52xx_can_clock_freq(struct of_device *of, |
| 93 | int clock_src) |
| 94 | { |
| 95 | unsigned int pvr; |
| 96 | |
| 97 | pvr = mfspr(SPRN_PVR); |
| 98 | |
| 99 | if (clock_src == MSCAN_CLKSRC_BUS || pvr == 0x80822011) |
| 100 | return mpc5xxx_get_bus_frequency(of->node); |
| 101 | |
| 102 | return mpc52xx_can_xtal_freq(of); |
| 103 | } |
| 104 | |
| 105 | static int __devinit mpc5xxx_can_probe(struct of_device *ofdev, |
| 106 | const struct of_device_id *id) |
| 107 | { |
| 108 | struct device_node *np = ofdev->node; |
| 109 | struct net_device *dev; |
| 110 | struct mscan_priv *priv; |
| 111 | void __iomem *base; |
| 112 | const char *clk_src; |
| 113 | int err, irq, clock_src; |
| 114 | |
| 115 | base = of_iomap(ofdev->node, 0); |
| 116 | if (!base) { |
| 117 | dev_err(&ofdev->dev, "couldn't ioremap\n"); |
| 118 | err = -ENOMEM; |
| 119 | goto exit_release_mem; |
| 120 | } |
| 121 | |
| 122 | irq = irq_of_parse_and_map(np, 0); |
| 123 | if (!irq) { |
| 124 | dev_err(&ofdev->dev, "no irq found\n"); |
| 125 | err = -ENODEV; |
| 126 | goto exit_unmap_mem; |
| 127 | } |
| 128 | |
| 129 | dev = alloc_mscandev(); |
| 130 | if (!dev) { |
| 131 | err = -ENOMEM; |
| 132 | goto exit_dispose_irq; |
| 133 | } |
| 134 | |
| 135 | priv = netdev_priv(dev); |
| 136 | priv->reg_base = base; |
| 137 | dev->irq = irq; |
| 138 | |
| 139 | /* |
| 140 | * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock |
| 141 | * (IP_CLK) can be selected as MSCAN clock source. According to |
| 142 | * the MPC5200 user's manual, the oscillator clock is the better |
| 143 | * choice as it has less jitter. For this reason, it is selected |
| 144 | * by default. |
| 145 | */ |
| 146 | clk_src = of_get_property(np, "fsl,mscan-clk-src", NULL); |
| 147 | if (clk_src && strcmp(clk_src, "ip") == 0) |
| 148 | clock_src = MSCAN_CLKSRC_BUS; |
| 149 | else |
| 150 | clock_src = MSCAN_CLKSRC_XTAL; |
| 151 | priv->can.clock.freq = mpc52xx_can_clock_freq(ofdev, clock_src); |
| 152 | if (!priv->can.clock.freq) { |
| 153 | dev_err(&ofdev->dev, "couldn't get MSCAN clock frequency\n"); |
| 154 | err = -ENODEV; |
| 155 | goto exit_free_mscan; |
| 156 | } |
| 157 | |
| 158 | SET_NETDEV_DEV(dev, &ofdev->dev); |
| 159 | |
| 160 | err = register_mscandev(dev, clock_src); |
| 161 | if (err) { |
| 162 | dev_err(&ofdev->dev, "registering %s failed (err=%d)\n", |
| 163 | DRV_NAME, err); |
| 164 | goto exit_free_mscan; |
| 165 | } |
| 166 | |
| 167 | dev_set_drvdata(&ofdev->dev, dev); |
| 168 | |
| 169 | dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n", |
| 170 | priv->reg_base, dev->irq, priv->can.clock.freq); |
| 171 | |
| 172 | return 0; |
| 173 | |
| 174 | exit_free_mscan: |
| 175 | free_candev(dev); |
| 176 | exit_dispose_irq: |
| 177 | irq_dispose_mapping(irq); |
| 178 | exit_unmap_mem: |
| 179 | iounmap(base); |
| 180 | exit_release_mem: |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | static int __devexit mpc5xxx_can_remove(struct of_device *ofdev) |
| 185 | { |
| 186 | struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 187 | struct mscan_priv *priv = netdev_priv(dev); |
| 188 | |
| 189 | dev_set_drvdata(&ofdev->dev, NULL); |
| 190 | |
| 191 | unregister_mscandev(dev); |
| 192 | iounmap(priv->reg_base); |
| 193 | irq_dispose_mapping(dev->irq); |
| 194 | free_candev(dev); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | #ifdef CONFIG_PM |
| 200 | static struct mscan_regs saved_regs; |
| 201 | static int mpc5xxx_can_suspend(struct of_device *ofdev, pm_message_t state) |
| 202 | { |
| 203 | struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 204 | struct mscan_priv *priv = netdev_priv(dev); |
| 205 | struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base; |
| 206 | |
| 207 | _memcpy_fromio(&saved_regs, regs, sizeof(*regs)); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int mpc5xxx_can_resume(struct of_device *ofdev) |
| 213 | { |
| 214 | struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 215 | struct mscan_priv *priv = netdev_priv(dev); |
| 216 | struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base; |
| 217 | |
| 218 | regs->canctl0 |= MSCAN_INITRQ; |
Wolfram Sang | 0285e7c | 2009-11-16 12:57:45 +0000 | [diff] [blame] | 219 | while (!(regs->canctl1 & MSCAN_INITAK)) |
Wolfram Sang | afa17a5 | 2009-11-13 06:14:52 +0000 | [diff] [blame] | 220 | udelay(10); |
| 221 | |
| 222 | regs->canctl1 = saved_regs.canctl1; |
| 223 | regs->canbtr0 = saved_regs.canbtr0; |
| 224 | regs->canbtr1 = saved_regs.canbtr1; |
| 225 | regs->canidac = saved_regs.canidac; |
| 226 | |
| 227 | /* restore masks, buffers etc. */ |
| 228 | _memcpy_toio(®s->canidar1_0, (void *)&saved_regs.canidar1_0, |
| 229 | sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0)); |
| 230 | |
| 231 | regs->canctl0 &= ~MSCAN_INITRQ; |
| 232 | regs->cantbsel = saved_regs.cantbsel; |
| 233 | regs->canrier = saved_regs.canrier; |
| 234 | regs->cantier = saved_regs.cantier; |
| 235 | regs->canctl0 = saved_regs.canctl0; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | #endif |
| 240 | |
| 241 | static struct of_device_id __devinitdata mpc5xxx_can_table[] = { |
| 242 | {.compatible = "fsl,mpc5200-mscan"}, |
| 243 | {.compatible = "fsl,mpc5200b-mscan"}, |
| 244 | {}, |
| 245 | }; |
| 246 | |
| 247 | static struct of_platform_driver mpc5xxx_can_driver = { |
| 248 | .owner = THIS_MODULE, |
| 249 | .name = "mpc5xxx_can", |
| 250 | .probe = mpc5xxx_can_probe, |
| 251 | .remove = __devexit_p(mpc5xxx_can_remove), |
| 252 | #ifdef CONFIG_PM |
| 253 | .suspend = mpc5xxx_can_suspend, |
| 254 | .resume = mpc5xxx_can_resume, |
| 255 | #endif |
| 256 | .match_table = mpc5xxx_can_table, |
| 257 | }; |
| 258 | |
| 259 | static int __init mpc5xxx_can_init(void) |
| 260 | { |
| 261 | return of_register_platform_driver(&mpc5xxx_can_driver); |
| 262 | } |
| 263 | module_init(mpc5xxx_can_init); |
| 264 | |
| 265 | static void __exit mpc5xxx_can_exit(void) |
| 266 | { |
| 267 | return of_unregister_platform_driver(&mpc5xxx_can_driver); |
| 268 | }; |
| 269 | module_exit(mpc5xxx_can_exit); |
| 270 | |
| 271 | MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); |
| 272 | MODULE_DESCRIPTION("Freescale MPC5200 CAN driver"); |
| 273 | MODULE_LICENSE("GPL v2"); |