uio_fsl_elbc_gpcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* uio_fsl_elbc_gpcm: UIO driver for eLBC/GPCM peripherals
  2. Copyright (C) 2014 Linutronix GmbH
  3. Author: John Ogness <john.ogness@linutronix.de>
  4. This driver provides UIO access to memory of a peripheral connected
  5. to the Freescale enhanced local bus controller (eLBC) interface
  6. using the general purpose chip-select mode (GPCM).
  7. Here is an example of the device tree entries:
  8. localbus@ffe05000 {
  9. ranges = <0x2 0x0 0x0 0xff810000 0x10000>;
  10. dpm@2,0 {
  11. compatible = "fsl,elbc-gpcm-uio";
  12. reg = <0x2 0x0 0x10000>;
  13. elbc-gpcm-br = <0xff810800>;
  14. elbc-gpcm-or = <0xffff09f7>;
  15. interrupt-parent = <&mpic>;
  16. interrupts = <4 1>;
  17. device_type = "netx5152";
  18. uio_name = "netx_custom";
  19. netx5152,init-win0-offset = <0x0>;
  20. };
  21. };
  22. Only the entries reg (to identify bank) and elbc-gpcm-* (initial BR/OR
  23. values) are required. The entries interrupt*, device_type, and uio_name
  24. are optional (as well as any type-specific options such as
  25. netx5152,init-win0-offset). As long as no interrupt handler is needed,
  26. this driver can be used without any type-specific implementation.
  27. The netx5152 type has been tested to work with the netX 51/52 hardware
  28. from Hilscher using the Hilscher userspace netX stack.
  29. The netx5152 type should serve as a model to add new type-specific
  30. devices as needed.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/device.h>
  34. #include <linux/string.h>
  35. #include <linux/slab.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/uio_driver.h>
  38. #include <linux/of_address.h>
  39. #include <linux/of_irq.h>
  40. #include <asm/fsl_lbc.h>
  41. #define MAX_BANKS 8
  42. struct fsl_elbc_gpcm {
  43. struct device *dev;
  44. struct fsl_lbc_regs __iomem *lbc;
  45. u32 bank;
  46. const char *name;
  47. void (*init)(struct uio_info *info);
  48. void (*shutdown)(struct uio_info *info, bool init_err);
  49. irqreturn_t (*irq_handler)(int irq, struct uio_info *info);
  50. };
  51. static ssize_t reg_show(struct device *dev, struct device_attribute *attr,
  52. char *buf);
  53. static ssize_t reg_store(struct device *dev, struct device_attribute *attr,
  54. const char *buf, size_t count);
  55. DEVICE_ATTR(reg_br, S_IRUGO|S_IWUSR|S_IWGRP, reg_show, reg_store);
  56. DEVICE_ATTR(reg_or, S_IRUGO|S_IWUSR|S_IWGRP, reg_show, reg_store);
  57. static ssize_t reg_show(struct device *dev, struct device_attribute *attr,
  58. char *buf)
  59. {
  60. struct platform_device *pdev = to_platform_device(dev);
  61. struct uio_info *info = platform_get_drvdata(pdev);
  62. struct fsl_elbc_gpcm *priv = info->priv;
  63. struct fsl_lbc_bank *bank = &priv->lbc->bank[priv->bank];
  64. if (attr == &dev_attr_reg_br) {
  65. return scnprintf(buf, PAGE_SIZE, "0x%08x\n",
  66. in_be32(&bank->br));
  67. } else if (attr == &dev_attr_reg_or) {
  68. return scnprintf(buf, PAGE_SIZE, "0x%08x\n",
  69. in_be32(&bank->or));
  70. }
  71. return 0;
  72. }
  73. static ssize_t reg_store(struct device *dev, struct device_attribute *attr,
  74. const char *buf, size_t count)
  75. {
  76. struct platform_device *pdev = to_platform_device(dev);
  77. struct uio_info *info = platform_get_drvdata(pdev);
  78. struct fsl_elbc_gpcm *priv = info->priv;
  79. struct fsl_lbc_bank *bank = &priv->lbc->bank[priv->bank];
  80. unsigned long val;
  81. u32 reg_br_cur;
  82. u32 reg_or_cur;
  83. u32 reg_new;
  84. /* parse use input */
  85. if (kstrtoul(buf, 0, &val) != 0)
  86. return -EINVAL;
  87. reg_new = (u32)val;
  88. /* read current values */
  89. reg_br_cur = in_be32(&bank->br);
  90. reg_or_cur = in_be32(&bank->or);
  91. if (attr == &dev_attr_reg_br) {
  92. /* not allowed to change effective base address */
  93. if ((reg_br_cur & reg_or_cur & BR_BA) !=
  94. (reg_new & reg_or_cur & BR_BA)) {
  95. return -EINVAL;
  96. }
  97. /* not allowed to change mode */
  98. if ((reg_new & BR_MSEL) != BR_MS_GPCM)
  99. return -EINVAL;
  100. /* write new value (force valid) */
  101. out_be32(&bank->br, reg_new | BR_V);
  102. } else if (attr == &dev_attr_reg_or) {
  103. /* not allowed to change access mask */
  104. if ((reg_or_cur & OR_GPCM_AM) != (reg_new & OR_GPCM_AM))
  105. return -EINVAL;
  106. /* write new value */
  107. out_be32(&bank->or, reg_new);
  108. } else {
  109. return -EINVAL;
  110. }
  111. return count;
  112. }
  113. #ifdef CONFIG_UIO_FSL_ELBC_GPCM_NETX5152
  114. #define DPM_HOST_WIN0_OFFSET 0xff00
  115. #define DPM_HOST_INT_STAT0 0xe0
  116. #define DPM_HOST_INT_EN0 0xf0
  117. #define DPM_HOST_INT_MASK 0xe600ffff
  118. #define DPM_HOST_INT_GLOBAL_EN 0x80000000
  119. static irqreturn_t netx5152_irq_handler(int irq, struct uio_info *info)
  120. {
  121. void __iomem *reg_int_en = info->mem[0].internal_addr +
  122. DPM_HOST_WIN0_OFFSET +
  123. DPM_HOST_INT_EN0;
  124. void __iomem *reg_int_stat = info->mem[0].internal_addr +
  125. DPM_HOST_WIN0_OFFSET +
  126. DPM_HOST_INT_STAT0;
  127. /* check if an interrupt is enabled and active */
  128. if ((ioread32(reg_int_en) & ioread32(reg_int_stat) &
  129. DPM_HOST_INT_MASK) == 0) {
  130. return IRQ_NONE;
  131. }
  132. /* disable interrupts */
  133. iowrite32(ioread32(reg_int_en) & ~DPM_HOST_INT_GLOBAL_EN, reg_int_en);
  134. return IRQ_HANDLED;
  135. }
  136. static void netx5152_init(struct uio_info *info)
  137. {
  138. unsigned long win0_offset = DPM_HOST_WIN0_OFFSET;
  139. struct fsl_elbc_gpcm *priv = info->priv;
  140. const void *prop;
  141. /* get an optional initial win0 offset */
  142. prop = of_get_property(priv->dev->of_node,
  143. "netx5152,init-win0-offset", NULL);
  144. if (prop)
  145. win0_offset = of_read_ulong(prop, 1);
  146. /* disable interrupts */
  147. iowrite32(0, info->mem[0].internal_addr + win0_offset +
  148. DPM_HOST_INT_EN0);
  149. }
  150. static void netx5152_shutdown(struct uio_info *info, bool init_err)
  151. {
  152. if (init_err)
  153. return;
  154. /* disable interrupts */
  155. iowrite32(0, info->mem[0].internal_addr + DPM_HOST_WIN0_OFFSET +
  156. DPM_HOST_INT_EN0);
  157. }
  158. #endif
  159. static void setup_periph(struct fsl_elbc_gpcm *priv,
  160. const char *type)
  161. {
  162. #ifdef CONFIG_UIO_FSL_ELBC_GPCM_NETX5152
  163. if (strcmp(type, "netx5152") == 0) {
  164. priv->irq_handler = netx5152_irq_handler;
  165. priv->init = netx5152_init;
  166. priv->shutdown = netx5152_shutdown;
  167. priv->name = "netX 51/52";
  168. return;
  169. }
  170. #endif
  171. }
  172. static int check_of_data(struct fsl_elbc_gpcm *priv,
  173. struct resource *res,
  174. u32 reg_br, u32 reg_or)
  175. {
  176. /* check specified bank */
  177. if (priv->bank >= MAX_BANKS) {
  178. dev_err(priv->dev, "invalid bank\n");
  179. return -ENODEV;
  180. }
  181. /* check specified mode (BR_MS_GPCM is 0) */
  182. if ((reg_br & BR_MSEL) != BR_MS_GPCM) {
  183. dev_err(priv->dev, "unsupported mode\n");
  184. return -ENODEV;
  185. }
  186. /* check specified mask vs. resource size */
  187. if ((~(reg_or & OR_GPCM_AM) + 1) != resource_size(res)) {
  188. dev_err(priv->dev, "address mask / size mismatch\n");
  189. return -ENODEV;
  190. }
  191. /* check specified address */
  192. if ((reg_br & reg_or & BR_BA) != fsl_lbc_addr(res->start)) {
  193. dev_err(priv->dev, "base address mismatch\n");
  194. return -ENODEV;
  195. }
  196. return 0;
  197. }
  198. static int get_of_data(struct fsl_elbc_gpcm *priv, struct device_node *node,
  199. struct resource *res, u32 *reg_br,
  200. u32 *reg_or, unsigned int *irq, char **name)
  201. {
  202. const char *dt_name;
  203. const char *type;
  204. int ret;
  205. /* get the memory resource */
  206. ret = of_address_to_resource(node, 0, res);
  207. if (ret) {
  208. dev_err(priv->dev, "failed to get resource\n");
  209. return ret;
  210. }
  211. /* get the bank number */
  212. ret = of_property_read_u32(node, "reg", &priv->bank);
  213. if (ret) {
  214. dev_err(priv->dev, "failed to get bank number\n");
  215. return ret;
  216. }
  217. /* get BR value to set */
  218. ret = of_property_read_u32(node, "elbc-gpcm-br", reg_br);
  219. if (ret) {
  220. dev_err(priv->dev, "missing elbc-gpcm-br value\n");
  221. return ret;
  222. }
  223. /* get OR value to set */
  224. ret = of_property_read_u32(node, "elbc-gpcm-or", reg_or);
  225. if (ret) {
  226. dev_err(priv->dev, "missing elbc-gpcm-or value\n");
  227. return ret;
  228. }
  229. /* get optional peripheral type */
  230. priv->name = "generic";
  231. if (of_property_read_string(node, "device_type", &type) == 0)
  232. setup_periph(priv, type);
  233. /* get optional irq value */
  234. *irq = irq_of_parse_and_map(node, 0);
  235. /* sanity check device tree data */
  236. ret = check_of_data(priv, res, *reg_br, *reg_or);
  237. if (ret)
  238. return ret;
  239. /* get optional uio name */
  240. if (of_property_read_string(node, "uio_name", &dt_name) != 0)
  241. dt_name = "eLBC_GPCM";
  242. *name = kstrdup(dt_name, GFP_KERNEL);
  243. if (!*name)
  244. return -ENOMEM;
  245. return 0;
  246. }
  247. static int uio_fsl_elbc_gpcm_probe(struct platform_device *pdev)
  248. {
  249. struct device_node *node = pdev->dev.of_node;
  250. struct fsl_elbc_gpcm *priv;
  251. struct uio_info *info;
  252. char *uio_name = NULL;
  253. struct resource res;
  254. unsigned int irq;
  255. u32 reg_br_cur;
  256. u32 reg_or_cur;
  257. u32 reg_br_new;
  258. u32 reg_or_new;
  259. int ret;
  260. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  261. return -ENODEV;
  262. /* allocate private data */
  263. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  264. if (!priv)
  265. return -ENOMEM;
  266. priv->dev = &pdev->dev;
  267. priv->lbc = fsl_lbc_ctrl_dev->regs;
  268. /* get device tree data */
  269. ret = get_of_data(priv, node, &res, &reg_br_new, &reg_or_new,
  270. &irq, &uio_name);
  271. if (ret)
  272. goto out_err0;
  273. /* allocate UIO structure */
  274. info = kzalloc(sizeof(*info), GFP_KERNEL);
  275. if (!info) {
  276. ret = -ENOMEM;
  277. goto out_err0;
  278. }
  279. /* get current BR/OR values */
  280. reg_br_cur = in_be32(&priv->lbc->bank[priv->bank].br);
  281. reg_or_cur = in_be32(&priv->lbc->bank[priv->bank].or);
  282. /* if bank already configured, make sure it matches */
  283. if ((reg_br_cur & BR_V)) {
  284. if ((reg_br_cur & BR_MSEL) != BR_MS_GPCM ||
  285. (reg_br_cur & reg_or_cur & BR_BA)
  286. != fsl_lbc_addr(res.start)) {
  287. dev_err(priv->dev,
  288. "bank in use by another peripheral\n");
  289. ret = -ENODEV;
  290. goto out_err1;
  291. }
  292. /* warn if behavior settings changing */
  293. if ((reg_br_cur & ~(BR_BA | BR_V)) !=
  294. (reg_br_new & ~(BR_BA | BR_V))) {
  295. dev_warn(priv->dev,
  296. "modifying BR settings: 0x%08x -> 0x%08x",
  297. reg_br_cur, reg_br_new);
  298. }
  299. if ((reg_or_cur & ~OR_GPCM_AM) != (reg_or_new & ~OR_GPCM_AM)) {
  300. dev_warn(priv->dev,
  301. "modifying OR settings: 0x%08x -> 0x%08x",
  302. reg_or_cur, reg_or_new);
  303. }
  304. }
  305. /* configure the bank (force base address and GPCM) */
  306. reg_br_new &= ~(BR_BA | BR_MSEL);
  307. reg_br_new |= fsl_lbc_addr(res.start) | BR_MS_GPCM | BR_V;
  308. out_be32(&priv->lbc->bank[priv->bank].or, reg_or_new);
  309. out_be32(&priv->lbc->bank[priv->bank].br, reg_br_new);
  310. /* map the memory resource */
  311. info->mem[0].internal_addr = ioremap(res.start, resource_size(&res));
  312. if (!info->mem[0].internal_addr) {
  313. dev_err(priv->dev, "failed to map chip region\n");
  314. ret = -ENODEV;
  315. goto out_err1;
  316. }
  317. /* set all UIO data */
  318. if (node->name)
  319. info->mem[0].name = kstrdup(node->name, GFP_KERNEL);
  320. info->mem[0].addr = res.start;
  321. info->mem[0].size = resource_size(&res);
  322. info->mem[0].memtype = UIO_MEM_PHYS;
  323. info->priv = priv;
  324. info->name = uio_name;
  325. info->version = "0.0.1";
  326. if (irq != NO_IRQ) {
  327. if (priv->irq_handler) {
  328. info->irq = irq;
  329. info->irq_flags = IRQF_SHARED;
  330. info->handler = priv->irq_handler;
  331. } else {
  332. irq = NO_IRQ;
  333. dev_warn(priv->dev, "ignoring irq, no handler\n");
  334. }
  335. }
  336. if (priv->init)
  337. priv->init(info);
  338. /* register UIO device */
  339. if (uio_register_device(priv->dev, info) != 0) {
  340. dev_err(priv->dev, "UIO registration failed\n");
  341. ret = -ENODEV;
  342. goto out_err2;
  343. }
  344. /* store private data */
  345. platform_set_drvdata(pdev, info);
  346. /* create sysfs files */
  347. ret = device_create_file(priv->dev, &dev_attr_reg_br);
  348. if (ret)
  349. goto out_err3;
  350. ret = device_create_file(priv->dev, &dev_attr_reg_or);
  351. if (ret)
  352. goto out_err4;
  353. dev_info(priv->dev,
  354. "eLBC/GPCM device (%s) at 0x%llx, bank %d, irq=%d\n",
  355. priv->name, (unsigned long long)res.start, priv->bank,
  356. irq != NO_IRQ ? irq : -1);
  357. return 0;
  358. out_err4:
  359. device_remove_file(priv->dev, &dev_attr_reg_br);
  360. out_err3:
  361. platform_set_drvdata(pdev, NULL);
  362. uio_unregister_device(info);
  363. out_err2:
  364. if (priv->shutdown)
  365. priv->shutdown(info, true);
  366. iounmap(info->mem[0].internal_addr);
  367. out_err1:
  368. kfree(info->mem[0].name);
  369. kfree(info);
  370. out_err0:
  371. kfree(uio_name);
  372. kfree(priv);
  373. return ret;
  374. }
  375. static int uio_fsl_elbc_gpcm_remove(struct platform_device *pdev)
  376. {
  377. struct uio_info *info = platform_get_drvdata(pdev);
  378. struct fsl_elbc_gpcm *priv = info->priv;
  379. device_remove_file(priv->dev, &dev_attr_reg_or);
  380. device_remove_file(priv->dev, &dev_attr_reg_br);
  381. platform_set_drvdata(pdev, NULL);
  382. uio_unregister_device(info);
  383. if (priv->shutdown)
  384. priv->shutdown(info, false);
  385. iounmap(info->mem[0].internal_addr);
  386. kfree(info->mem[0].name);
  387. kfree(info->name);
  388. kfree(info);
  389. kfree(priv);
  390. return 0;
  391. }
  392. static const struct of_device_id uio_fsl_elbc_gpcm_match[] = {
  393. { .compatible = "fsl,elbc-gpcm-uio", },
  394. {}
  395. };
  396. MODULE_DEVICE_TABLE(of, uio_fsl_elbc_gpcm_match);
  397. static struct platform_driver uio_fsl_elbc_gpcm_driver = {
  398. .driver = {
  399. .name = "fsl,elbc-gpcm-uio",
  400. .owner = THIS_MODULE,
  401. .of_match_table = uio_fsl_elbc_gpcm_match,
  402. },
  403. .probe = uio_fsl_elbc_gpcm_probe,
  404. .remove = uio_fsl_elbc_gpcm_remove,
  405. };
  406. module_platform_driver(uio_fsl_elbc_gpcm_driver);
  407. MODULE_LICENSE("GPL");
  408. MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
  409. MODULE_DESCRIPTION("Freescale Enhanced Local Bus Controller GPCM driver");