|
| 1 | +/* |
| 2 | + * CAN bus driver for the Freescale MPC5xxx embedded CPU. |
| 3 | + * |
| 4 | + * Copyright (C) 2004-2005 Andrey Volkov <[email protected]>, |
| 5 | + * Varma Electronics Oy |
| 6 | + * Copyright (C) 2008-2009 Wolfgang Grandegger <[email protected]> |
| 7 | + * Copyright (C) 2009 Wolfram Sang, Pengutronix <[email protected]> |
| 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 | + |
| 37 | + |
| 38 | +#define DRV_NAME "mpc5xxx_can" |
| 39 | + |
| 40 | +static struct of_device_id mpc52xx_cdm_ids[] __devinitdata = { |
| 41 | + { .compatible = "fsl,mpc5200-cdm", }, |
| 42 | + { .compatible = "fsl,mpc5200b-cdm", }, |
| 43 | + {} |
| 44 | +}; |
| 45 | + |
| 46 | +/* |
| 47 | + * Get the frequency of the external oscillator clock connected |
| 48 | + * to the SYS_XTAL_IN pin, or return 0 if it cannot be determined. |
| 49 | + */ |
| 50 | +static unsigned int __devinit mpc52xx_can_xtal_freq(struct of_device *of) |
| 51 | +{ |
| 52 | + struct mpc52xx_cdm __iomem *cdm; |
| 53 | + struct device_node *np_cdm; |
| 54 | + unsigned int freq; |
| 55 | + u32 val; |
| 56 | + |
| 57 | + freq = mpc5xxx_get_bus_frequency(of->node); |
| 58 | + if (!freq) |
| 59 | + return 0; |
| 60 | + |
| 61 | + /* |
| 62 | + * Determine SYS_XTAL_IN frequency from the clock domain settings |
| 63 | + */ |
| 64 | + np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids); |
| 65 | + if (!np_cdm) { |
| 66 | + dev_err(&of->dev, "can't get clock node!\n"); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + cdm = of_iomap(np_cdm, 0); |
| 70 | + of_node_put(np_cdm); |
| 71 | + |
| 72 | + if (in_8(&cdm->ipb_clk_sel) & 0x1) |
| 73 | + freq *= 2; |
| 74 | + val = in_be32(&cdm->rstcfg); |
| 75 | + if (val & (1 << 5)) |
| 76 | + freq *= 8; |
| 77 | + else |
| 78 | + freq *= 4; |
| 79 | + if (val & (1 << 6)) |
| 80 | + freq /= 12; |
| 81 | + else |
| 82 | + freq /= 16; |
| 83 | + |
| 84 | + iounmap(cdm); |
| 85 | + |
| 86 | + return freq; |
| 87 | +} |
| 88 | + |
| 89 | +/* |
| 90 | + * Get frequency of the MSCAN clock source |
| 91 | + * |
| 92 | + * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock (IP_CLK) |
| 93 | + * can be selected. According to the MPC5200 user's manual, the oscillator |
| 94 | + * clock is the better choice as it has less jitter but due to a hardware |
| 95 | + * bug, it can not be selected for the old MPC5200 Rev. A chips. |
| 96 | + */ |
| 97 | + |
| 98 | +static unsigned int __devinit mpc52xx_can_clock_freq(struct of_device *of, |
| 99 | + int clock_src) |
| 100 | +{ |
| 101 | + unsigned int pvr; |
| 102 | + |
| 103 | + pvr = mfspr(SPRN_PVR); |
| 104 | + |
| 105 | + if (clock_src == MSCAN_CLKSRC_BUS || pvr == 0x80822011) |
| 106 | + return mpc5xxx_get_bus_frequency(of->node); |
| 107 | + |
| 108 | + return mpc52xx_can_xtal_freq(of); |
| 109 | +} |
| 110 | + |
| 111 | +static int __devinit mpc5xxx_can_probe(struct of_device *ofdev, |
| 112 | + const struct of_device_id *id) |
| 113 | +{ |
| 114 | + struct device_node *np = ofdev->node; |
| 115 | + struct net_device *dev; |
| 116 | + struct mscan_priv *priv; |
| 117 | + void __iomem *base; |
| 118 | + const char *clk_src; |
| 119 | + int err, irq, clock_src; |
| 120 | + |
| 121 | + base = of_iomap(ofdev->node, 0); |
| 122 | + if (!base) { |
| 123 | + dev_err(&ofdev->dev, "couldn't ioremap\n"); |
| 124 | + err = -ENOMEM; |
| 125 | + goto exit_release_mem; |
| 126 | + } |
| 127 | + |
| 128 | + irq = irq_of_parse_and_map(np, 0); |
| 129 | + if (!irq) { |
| 130 | + dev_err(&ofdev->dev, "no irq found\n"); |
| 131 | + err = -ENODEV; |
| 132 | + goto exit_unmap_mem; |
| 133 | + } |
| 134 | + |
| 135 | + dev = alloc_mscandev(); |
| 136 | + if (!dev) { |
| 137 | + err = -ENOMEM; |
| 138 | + goto exit_dispose_irq; |
| 139 | + } |
| 140 | + |
| 141 | + priv = netdev_priv(dev); |
| 142 | + priv->reg_base = base; |
| 143 | + dev->irq = irq; |
| 144 | + |
| 145 | + /* |
| 146 | + * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock |
| 147 | + * (IP_CLK) can be selected as MSCAN clock source. According to |
| 148 | + * the MPC5200 user's manual, the oscillator clock is the better |
| 149 | + * choice as it has less jitter. For this reason, it is selected |
| 150 | + * by default. |
| 151 | + */ |
| 152 | + clk_src = of_get_property(np, "fsl,mscan-clk-src", NULL); |
| 153 | + if (clk_src && strcmp(clk_src, "ip") == 0) |
| 154 | + clock_src = MSCAN_CLKSRC_BUS; |
| 155 | + else |
| 156 | + clock_src = MSCAN_CLKSRC_XTAL; |
| 157 | + priv->can.clock.freq = mpc52xx_can_clock_freq(ofdev, clock_src); |
| 158 | + if (!priv->can.clock.freq) { |
| 159 | + dev_err(&ofdev->dev, "couldn't get MSCAN clock frequency\n"); |
| 160 | + err = -ENODEV; |
| 161 | + goto exit_free_mscan; |
| 162 | + } |
| 163 | + |
| 164 | + SET_NETDEV_DEV(dev, &ofdev->dev); |
| 165 | + |
| 166 | + err = register_mscandev(dev, clock_src); |
| 167 | + if (err) { |
| 168 | + dev_err(&ofdev->dev, "registering %s failed (err=%d)\n", |
| 169 | + DRV_NAME, err); |
| 170 | + goto exit_free_mscan; |
| 171 | + } |
| 172 | + |
| 173 | + dev_set_drvdata(&ofdev->dev, dev); |
| 174 | + |
| 175 | + dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n", |
| 176 | + priv->reg_base, dev->irq, priv->can.clock.freq); |
| 177 | + |
| 178 | + return 0; |
| 179 | + |
| 180 | +exit_free_mscan: |
| 181 | + free_candev(dev); |
| 182 | +exit_dispose_irq: |
| 183 | + irq_dispose_mapping(irq); |
| 184 | +exit_unmap_mem: |
| 185 | + iounmap(base); |
| 186 | +exit_release_mem: |
| 187 | + return err; |
| 188 | +} |
| 189 | + |
| 190 | +static int __devexit mpc5xxx_can_remove(struct of_device *ofdev) |
| 191 | +{ |
| 192 | + struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 193 | + struct mscan_priv *priv = netdev_priv(dev); |
| 194 | + |
| 195 | + dev_set_drvdata(&ofdev->dev, NULL); |
| 196 | + |
| 197 | + unregister_mscandev(dev); |
| 198 | + iounmap(priv->reg_base); |
| 199 | + irq_dispose_mapping(dev->irq); |
| 200 | + free_candev(dev); |
| 201 | + |
| 202 | + return 0; |
| 203 | +} |
| 204 | + |
| 205 | +#ifdef CONFIG_PM |
| 206 | +static struct mscan_regs saved_regs; |
| 207 | +static int mpc5xxx_can_suspend(struct of_device *ofdev, pm_message_t state) |
| 208 | +{ |
| 209 | + struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 210 | + struct mscan_priv *priv = netdev_priv(dev); |
| 211 | + struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base; |
| 212 | + |
| 213 | + _memcpy_fromio(&saved_regs, regs, sizeof(*regs)); |
| 214 | + |
| 215 | + return 0; |
| 216 | +} |
| 217 | + |
| 218 | +static int mpc5xxx_can_resume(struct of_device *ofdev) |
| 219 | +{ |
| 220 | + struct net_device *dev = dev_get_drvdata(&ofdev->dev); |
| 221 | + struct mscan_priv *priv = netdev_priv(dev); |
| 222 | + struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base; |
| 223 | + |
| 224 | + regs->canctl0 |= MSCAN_INITRQ; |
| 225 | + while ((regs->canctl1 & MSCAN_INITAK) == 0) |
| 226 | + udelay(10); |
| 227 | + |
| 228 | + regs->canctl1 = saved_regs.canctl1; |
| 229 | + regs->canbtr0 = saved_regs.canbtr0; |
| 230 | + regs->canbtr1 = saved_regs.canbtr1; |
| 231 | + regs->canidac = saved_regs.canidac; |
| 232 | + |
| 233 | + /* restore masks, buffers etc. */ |
| 234 | + _memcpy_toio(®s->canidar1_0, (void *)&saved_regs.canidar1_0, |
| 235 | + sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0)); |
| 236 | + |
| 237 | + regs->canctl0 &= ~MSCAN_INITRQ; |
| 238 | + regs->cantbsel = saved_regs.cantbsel; |
| 239 | + regs->canrier = saved_regs.canrier; |
| 240 | + regs->cantier = saved_regs.cantier; |
| 241 | + regs->canctl0 = saved_regs.canctl0; |
| 242 | + |
| 243 | + return 0; |
| 244 | +} |
| 245 | +#endif |
| 246 | + |
| 247 | +static struct of_device_id __devinitdata mpc5xxx_can_table[] = { |
| 248 | + {.compatible = "fsl,mpc5200-mscan"}, |
| 249 | + {.compatible = "fsl,mpc5200b-mscan"}, |
| 250 | + {}, |
| 251 | +}; |
| 252 | + |
| 253 | +static struct of_platform_driver mpc5xxx_can_driver = { |
| 254 | + .owner = THIS_MODULE, |
| 255 | + .name = "mpc5xxx_can", |
| 256 | + .probe = mpc5xxx_can_probe, |
| 257 | + .remove = __devexit_p(mpc5xxx_can_remove), |
| 258 | +#ifdef CONFIG_PM |
| 259 | + .suspend = mpc5xxx_can_suspend, |
| 260 | + .resume = mpc5xxx_can_resume, |
| 261 | +#endif |
| 262 | + .match_table = mpc5xxx_can_table, |
| 263 | +}; |
| 264 | + |
| 265 | +static int __init mpc5xxx_can_init(void) |
| 266 | +{ |
| 267 | + return of_register_platform_driver(&mpc5xxx_can_driver); |
| 268 | +} |
| 269 | +module_init(mpc5xxx_can_init); |
| 270 | + |
| 271 | +static void __exit mpc5xxx_can_exit(void) |
| 272 | +{ |
| 273 | + return of_unregister_platform_driver(&mpc5xxx_can_driver); |
| 274 | +}; |
| 275 | +module_exit(mpc5xxx_can_exit); |
| 276 | + |
| 277 | +MODULE_AUTHOR( "Wolfgang Grandegger <[email protected]>"); |
| 278 | +MODULE_DESCRIPTION("Freescale MPC5200 CAN driver"); |
| 279 | +MODULE_LICENSE("GPL v2"); |
0 commit comments