sja1000_platform.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2005 Sascha Hauer, Pengutronix
  3. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the version 2 of the GNU General Public License
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/delay.h>
  22. #include <linux/pci.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/irq.h>
  25. #include <linux/can/dev.h>
  26. #include <linux/can/platform/sja1000.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/clk.h>
  32. #include "sja1000.h"
  33. #define DRV_NAME "sja1000_platform"
  34. #define SP_CAN_CLOCK (16000000 / 2)
  35. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  36. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  37. MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
  38. MODULE_ALIAS("platform:" DRV_NAME);
  39. MODULE_LICENSE("GPL v2");
  40. struct sja1000_of_data {
  41. size_t priv_sz;
  42. int (*init)(struct sja1000_priv *priv, struct device_node *of);
  43. };
  44. struct technologic_priv {
  45. spinlock_t io_lock;
  46. };
  47. static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
  48. {
  49. return ioread8(priv->reg_base + reg);
  50. }
  51. static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
  52. {
  53. iowrite8(val, priv->reg_base + reg);
  54. }
  55. static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
  56. {
  57. return ioread8(priv->reg_base + reg * 2);
  58. }
  59. static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
  60. {
  61. iowrite8(val, priv->reg_base + reg * 2);
  62. }
  63. static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
  64. {
  65. return ioread8(priv->reg_base + reg * 4);
  66. }
  67. static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
  68. {
  69. iowrite8(val, priv->reg_base + reg * 4);
  70. }
  71. static u8 sp_technologic_read_reg16(const struct sja1000_priv *priv, int reg)
  72. {
  73. struct technologic_priv *tp = priv->priv;
  74. unsigned long flags;
  75. u8 val;
  76. spin_lock_irqsave(&tp->io_lock, flags);
  77. iowrite16(reg, priv->reg_base + 0);
  78. val = ioread16(priv->reg_base + 2);
  79. spin_unlock_irqrestore(&tp->io_lock, flags);
  80. return val;
  81. }
  82. static void sp_technologic_write_reg16(const struct sja1000_priv *priv,
  83. int reg, u8 val)
  84. {
  85. struct technologic_priv *tp = priv->priv;
  86. unsigned long flags;
  87. spin_lock_irqsave(&tp->io_lock, flags);
  88. iowrite16(reg, priv->reg_base + 0);
  89. iowrite16(val, priv->reg_base + 2);
  90. spin_unlock_irqrestore(&tp->io_lock, flags);
  91. }
  92. static int sp_technologic_init(struct sja1000_priv *priv, struct device_node *of)
  93. {
  94. struct technologic_priv *tp = priv->priv;
  95. priv->read_reg = sp_technologic_read_reg16;
  96. priv->write_reg = sp_technologic_write_reg16;
  97. spin_lock_init(&tp->io_lock);
  98. return 0;
  99. }
  100. static void sp_populate(struct sja1000_priv *priv,
  101. struct sja1000_platform_data *pdata,
  102. unsigned long resource_mem_flags)
  103. {
  104. /* The CAN clock frequency is half the oscillator clock frequency */
  105. priv->can.clock.freq = pdata->osc_freq / 2;
  106. priv->ocr = pdata->ocr;
  107. priv->cdr = pdata->cdr;
  108. switch (resource_mem_flags & IORESOURCE_MEM_TYPE_MASK) {
  109. case IORESOURCE_MEM_32BIT:
  110. priv->read_reg = sp_read_reg32;
  111. priv->write_reg = sp_write_reg32;
  112. break;
  113. case IORESOURCE_MEM_16BIT:
  114. priv->read_reg = sp_read_reg16;
  115. priv->write_reg = sp_write_reg16;
  116. break;
  117. case IORESOURCE_MEM_8BIT:
  118. default:
  119. priv->read_reg = sp_read_reg8;
  120. priv->write_reg = sp_write_reg8;
  121. break;
  122. }
  123. }
  124. static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
  125. {
  126. int err;
  127. u32 prop;
  128. u32 freq;
  129. err = of_property_read_u32(of, "reg-io-width", &prop);
  130. if (err)
  131. prop = 1; /* 8 bit is default */
  132. switch (prop) {
  133. case 4:
  134. priv->read_reg = sp_read_reg32;
  135. priv->write_reg = sp_write_reg32;
  136. break;
  137. case 2:
  138. priv->read_reg = sp_read_reg16;
  139. priv->write_reg = sp_write_reg16;
  140. break;
  141. case 1: /* fallthrough */
  142. default:
  143. priv->read_reg = sp_read_reg8;
  144. priv->write_reg = sp_write_reg8;
  145. }
  146. //ark1668e_devb
  147. if(priv->clk){
  148. freq = clk_get_rate(priv->clk);
  149. priv->can.clock.freq = freq / 2;
  150. //printk("----------------------->freq = %d priv->can.clock.freq = %d\n",freq,priv->can.clock.freq);
  151. }else{
  152. err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
  153. if (!err)
  154. priv->can.clock.freq = prop / 2;
  155. else
  156. priv->can.clock.freq = SP_CAN_CLOCK; /* default */
  157. //printk("----------------------->priv->can.clock.freq = %d \n",priv->can.clock.freq);
  158. }
  159. err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
  160. if (!err)
  161. priv->ocr |= prop & OCR_MODE_MASK;
  162. else
  163. priv->ocr |= OCR_MODE_NORMAL; /* default */
  164. err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
  165. if (!err)
  166. priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
  167. else
  168. priv->ocr |= OCR_TX0_PULLDOWN; /* default */
  169. err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
  170. if (!err && prop) {
  171. u32 divider = priv->can.clock.freq * 2 / prop;
  172. if (divider > 1)
  173. priv->cdr |= divider / 2 - 1;
  174. else
  175. priv->cdr |= CDR_CLKOUT_MASK;
  176. } else {
  177. priv->cdr |= CDR_CLK_OFF; /* default */
  178. }
  179. if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
  180. priv->cdr |= CDR_CBP; /* default */
  181. }
  182. static struct sja1000_of_data technologic_data = {
  183. .priv_sz = sizeof(struct technologic_priv),
  184. .init = sp_technologic_init,
  185. };
  186. static const struct of_device_id sp_of_table[] = {
  187. { .compatible = "nxp,sja1000", .data = NULL, },
  188. { .compatible = "technologic,sja1000", .data = &technologic_data, },
  189. { /* sentinel */ },
  190. };
  191. MODULE_DEVICE_TABLE(of, sp_of_table);
  192. static int sp_probe(struct platform_device *pdev)
  193. {
  194. int err, irq = 0;
  195. void __iomem *addr;
  196. struct net_device *dev;
  197. struct sja1000_priv *priv;
  198. struct resource *res_mem, *res_irq = NULL;
  199. struct sja1000_platform_data *pdata;
  200. struct device_node *of = pdev->dev.of_node;
  201. const struct of_device_id *of_id;
  202. const struct sja1000_of_data *of_data = NULL;
  203. size_t priv_sz = 0;
  204. pdata = dev_get_platdata(&pdev->dev);
  205. if (!pdata && !of) {
  206. dev_err(&pdev->dev, "No platform data provided!\n");
  207. return -ENODEV;
  208. }
  209. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  210. if (!res_mem)
  211. return -ENODEV;
  212. if (!devm_request_mem_region(&pdev->dev, res_mem->start,
  213. resource_size(res_mem), DRV_NAME))
  214. return -EBUSY;
  215. addr = devm_ioremap_nocache(&pdev->dev, res_mem->start,
  216. resource_size(res_mem));
  217. if (!addr)
  218. return -ENOMEM;
  219. if (of)
  220. irq = irq_of_parse_and_map(of, 0);
  221. else
  222. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  223. if (!irq && !res_irq)
  224. return -ENODEV;
  225. of_id = of_match_device(sp_of_table, &pdev->dev);
  226. if (of_id && of_id->data) {
  227. of_data = of_id->data;
  228. priv_sz = of_data->priv_sz;
  229. }
  230. dev = alloc_sja1000dev(priv_sz);
  231. if (!dev)
  232. return -ENOMEM;
  233. priv = netdev_priv(dev);
  234. if (res_irq) {
  235. irq = res_irq->start;
  236. priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
  237. if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
  238. priv->irq_flags |= IRQF_SHARED;
  239. } else {
  240. priv->irq_flags = IRQF_SHARED;
  241. }
  242. dev->irq = irq;
  243. priv->reg_base = addr;
  244. //ark1668e_devb
  245. priv->clk = of_clk_get(pdev->dev.of_node, 0);
  246. if (IS_ERR(priv->clk)){
  247. err = PTR_ERR(priv->clk);
  248. dev_err(&pdev->dev, "failed to get per clk: %d\n", err);
  249. //return PTR_ERR(priv->clk);
  250. }
  251. if (of) {
  252. sp_populate_of(priv, of);
  253. if (of_data && of_data->init) {
  254. err = of_data->init(priv, of);
  255. if (err)
  256. goto exit_free;
  257. }
  258. } else {
  259. sp_populate(priv, pdata, res_mem->flags);
  260. }
  261. platform_set_drvdata(pdev, dev);
  262. SET_NETDEV_DEV(dev, &pdev->dev);
  263. err = register_sja1000dev(dev);
  264. if (err) {
  265. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  266. DRV_NAME, err);
  267. goto exit_free;
  268. }
  269. dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
  270. DRV_NAME, priv->reg_base, dev->irq);
  271. return 0;
  272. exit_free:
  273. free_sja1000dev(dev);
  274. return err;
  275. }
  276. static int sp_remove(struct platform_device *pdev)
  277. {
  278. struct net_device *dev = platform_get_drvdata(pdev);
  279. unregister_sja1000dev(dev);
  280. free_sja1000dev(dev);
  281. return 0;
  282. }
  283. static struct platform_driver sp_driver = {
  284. .probe = sp_probe,
  285. .remove = sp_remove,
  286. .driver = {
  287. .name = DRV_NAME,
  288. .of_match_table = sp_of_table,
  289. },
  290. };
  291. module_platform_driver(sp_driver);