uio_fsl_elbc_gpcm.c 12 KB

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