core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * SuperH Pin Function Controller support.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #define DRV_NAME "sh-pfc"
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include "core.h"
  24. static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
  25. {
  26. struct resource *res;
  27. int k;
  28. if (pdev->num_resources == 0)
  29. return -EINVAL;
  30. pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
  31. sizeof(*pfc->window), GFP_NOWAIT);
  32. if (!pfc->window)
  33. return -ENOMEM;
  34. pfc->num_windows = pdev->num_resources;
  35. for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
  36. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  37. pfc->window[k].phys = res->start;
  38. pfc->window[k].size = resource_size(res);
  39. pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
  40. resource_size(res));
  41. if (!pfc->window[k].virt)
  42. return -ENOMEM;
  43. }
  44. return 0;
  45. }
  46. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  47. unsigned long address)
  48. {
  49. struct sh_pfc_window *window;
  50. unsigned int i;
  51. /* scan through physical windows and convert address */
  52. for (i = 0; i < pfc->num_windows; i++) {
  53. window = pfc->window + i;
  54. if (address < window->phys)
  55. continue;
  56. if (address >= (window->phys + window->size))
  57. continue;
  58. return window->virt + (address - window->phys);
  59. }
  60. BUG();
  61. }
  62. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  63. {
  64. unsigned int offset;
  65. unsigned int i;
  66. if (pfc->info->ranges == NULL)
  67. return pin;
  68. for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
  69. const struct pinmux_range *range = &pfc->info->ranges[i];
  70. if (pin <= range->end)
  71. return pin >= range->begin
  72. ? offset + pin - range->begin : -1;
  73. offset += range->end - range->begin + 1;
  74. }
  75. return -1;
  76. }
  77. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  78. {
  79. if (enum_id < r->begin)
  80. return 0;
  81. if (enum_id > r->end)
  82. return 0;
  83. return 1;
  84. }
  85. unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  86. unsigned long reg_width)
  87. {
  88. switch (reg_width) {
  89. case 8:
  90. return ioread8(mapped_reg);
  91. case 16:
  92. return ioread16(mapped_reg);
  93. case 32:
  94. return ioread32(mapped_reg);
  95. }
  96. BUG();
  97. return 0;
  98. }
  99. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
  100. unsigned long data)
  101. {
  102. switch (reg_width) {
  103. case 8:
  104. iowrite8(data, mapped_reg);
  105. return;
  106. case 16:
  107. iowrite16(data, mapped_reg);
  108. return;
  109. case 32:
  110. iowrite32(data, mapped_reg);
  111. return;
  112. }
  113. BUG();
  114. }
  115. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  116. struct pinmux_cfg_reg *crp,
  117. unsigned long in_pos,
  118. void __iomem **mapped_regp,
  119. unsigned long *maskp,
  120. unsigned long *posp)
  121. {
  122. int k;
  123. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  124. if (crp->field_width) {
  125. *maskp = (1 << crp->field_width) - 1;
  126. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  127. } else {
  128. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  129. *posp = crp->reg_width;
  130. for (k = 0; k <= in_pos; k++)
  131. *posp -= crp->var_field_width[k];
  132. }
  133. }
  134. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  135. struct pinmux_cfg_reg *crp,
  136. unsigned long field, unsigned long value)
  137. {
  138. void __iomem *mapped_reg;
  139. unsigned long mask, pos, data;
  140. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  141. pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
  142. "r_width = %ld, f_width = %ld\n",
  143. crp->reg, value, field, crp->reg_width, crp->field_width);
  144. mask = ~(mask << pos);
  145. value = value << pos;
  146. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  147. data &= mask;
  148. data |= value;
  149. if (pfc->info->unlock_reg)
  150. sh_pfc_write_raw_reg(
  151. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  152. ~data);
  153. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  154. }
  155. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  156. struct pinmux_cfg_reg **crp, int *fieldp,
  157. int *valuep)
  158. {
  159. struct pinmux_cfg_reg *config_reg;
  160. unsigned long r_width, f_width, curr_width, ncomb;
  161. int k, m, n, pos, bit_pos;
  162. k = 0;
  163. while (1) {
  164. config_reg = pfc->info->cfg_regs + k;
  165. r_width = config_reg->reg_width;
  166. f_width = config_reg->field_width;
  167. if (!r_width)
  168. break;
  169. pos = 0;
  170. m = 0;
  171. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  172. if (f_width)
  173. curr_width = f_width;
  174. else
  175. curr_width = config_reg->var_field_width[m];
  176. ncomb = 1 << curr_width;
  177. for (n = 0; n < ncomb; n++) {
  178. if (config_reg->enum_ids[pos + n] == enum_id) {
  179. *crp = config_reg;
  180. *fieldp = m;
  181. *valuep = n;
  182. return 0;
  183. }
  184. }
  185. pos += ncomb;
  186. m++;
  187. }
  188. k++;
  189. }
  190. return -1;
  191. }
  192. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
  193. pinmux_enum_t *enum_idp)
  194. {
  195. pinmux_enum_t *data = pfc->info->gpio_data;
  196. int k;
  197. if (pos) {
  198. *enum_idp = data[pos + 1];
  199. return pos + 1;
  200. }
  201. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  202. if (data[k] == mark) {
  203. *enum_idp = data[k + 1];
  204. return k + 1;
  205. }
  206. }
  207. pr_err("cannot locate data/mark enum_id for mark %d\n", mark);
  208. return -1;
  209. }
  210. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  211. {
  212. struct pinmux_cfg_reg *cr = NULL;
  213. pinmux_enum_t enum_id;
  214. struct pinmux_range *range;
  215. int in_range, pos, field, value;
  216. switch (pinmux_type) {
  217. case PINMUX_TYPE_FUNCTION:
  218. range = NULL;
  219. break;
  220. case PINMUX_TYPE_OUTPUT:
  221. range = &pfc->info->output;
  222. break;
  223. case PINMUX_TYPE_INPUT:
  224. range = &pfc->info->input;
  225. break;
  226. case PINMUX_TYPE_INPUT_PULLUP:
  227. range = &pfc->info->input_pu;
  228. break;
  229. case PINMUX_TYPE_INPUT_PULLDOWN:
  230. range = &pfc->info->input_pd;
  231. break;
  232. default:
  233. return -1;
  234. }
  235. pos = 0;
  236. enum_id = 0;
  237. field = 0;
  238. value = 0;
  239. while (1) {
  240. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  241. if (pos <= 0)
  242. return -1;
  243. if (!enum_id)
  244. break;
  245. /* first check if this is a function enum */
  246. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  247. if (!in_range) {
  248. /* not a function enum */
  249. if (range) {
  250. /*
  251. * other range exists, so this pin is
  252. * a regular GPIO pin that now is being
  253. * bound to a specific direction.
  254. *
  255. * for this case we only allow function enums
  256. * and the enums that match the other range.
  257. */
  258. in_range = sh_pfc_enum_in_range(enum_id, range);
  259. /*
  260. * special case pass through for fixed
  261. * input-only or output-only pins without
  262. * function enum register association.
  263. */
  264. if (in_range && enum_id == range->force)
  265. continue;
  266. } else {
  267. /*
  268. * no other range exists, so this pin
  269. * must then be of the function type.
  270. *
  271. * allow function type pins to select
  272. * any combination of function/in/out
  273. * in their MARK lists.
  274. */
  275. in_range = 1;
  276. }
  277. }
  278. if (!in_range)
  279. continue;
  280. if (sh_pfc_get_config_reg(pfc, enum_id, &cr,
  281. &field, &value) != 0)
  282. return -1;
  283. sh_pfc_write_config_reg(pfc, cr, field, value);
  284. }
  285. return 0;
  286. }
  287. static int sh_pfc_probe(struct platform_device *pdev)
  288. {
  289. struct sh_pfc_soc_info *info;
  290. struct sh_pfc *pfc;
  291. int ret;
  292. info = pdev->id_entry->driver_data
  293. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  294. if (info == NULL)
  295. return -ENODEV;
  296. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  297. if (pfc == NULL)
  298. return -ENOMEM;
  299. pfc->info = info;
  300. pfc->dev = &pdev->dev;
  301. ret = sh_pfc_ioremap(pfc, pdev);
  302. if (unlikely(ret < 0))
  303. return ret;
  304. spin_lock_init(&pfc->lock);
  305. pinctrl_provide_dummies();
  306. /*
  307. * Initialize pinctrl bindings first
  308. */
  309. ret = sh_pfc_register_pinctrl(pfc);
  310. if (unlikely(ret != 0))
  311. return ret;
  312. #ifdef CONFIG_GPIO_SH_PFC
  313. /*
  314. * Then the GPIO chip
  315. */
  316. ret = sh_pfc_register_gpiochip(pfc);
  317. if (unlikely(ret != 0)) {
  318. /*
  319. * If the GPIO chip fails to come up we still leave the
  320. * PFC state as it is, given that there are already
  321. * extant users of it that have succeeded by this point.
  322. */
  323. pr_notice("failed to init GPIO chip, ignoring...\n");
  324. }
  325. #endif
  326. platform_set_drvdata(pdev, pfc);
  327. pr_info("%s support registered\n", info->name);
  328. return 0;
  329. }
  330. static int sh_pfc_remove(struct platform_device *pdev)
  331. {
  332. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  333. #ifdef CONFIG_GPIO_SH_PFC
  334. sh_pfc_unregister_gpiochip(pfc);
  335. #endif
  336. sh_pfc_unregister_pinctrl(pfc);
  337. platform_set_drvdata(pdev, NULL);
  338. return 0;
  339. }
  340. static const struct platform_device_id sh_pfc_id_table[] = {
  341. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  342. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  343. #endif
  344. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  345. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  346. #endif
  347. #ifdef CONFIG_PINCTRL_PFC_SH7203
  348. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  349. #endif
  350. #ifdef CONFIG_PINCTRL_PFC_SH7264
  351. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  352. #endif
  353. #ifdef CONFIG_PINCTRL_PFC_SH7269
  354. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  355. #endif
  356. #ifdef CONFIG_PINCTRL_PFC_SH7372
  357. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  358. #endif
  359. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  360. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  361. #endif
  362. #ifdef CONFIG_PINCTRL_PFC_SH7720
  363. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  364. #endif
  365. #ifdef CONFIG_PINCTRL_PFC_SH7722
  366. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  367. #endif
  368. #ifdef CONFIG_PINCTRL_PFC_SH7723
  369. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  370. #endif
  371. #ifdef CONFIG_PINCTRL_PFC_SH7724
  372. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  373. #endif
  374. #ifdef CONFIG_PINCTRL_PFC_SH7734
  375. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  376. #endif
  377. #ifdef CONFIG_PINCTRL_PFC_SH7757
  378. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  379. #endif
  380. #ifdef CONFIG_PINCTRL_PFC_SH7785
  381. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  382. #endif
  383. #ifdef CONFIG_PINCTRL_PFC_SH7786
  384. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  385. #endif
  386. #ifdef CONFIG_PINCTRL_PFC_SHX3
  387. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  388. #endif
  389. { "sh-pfc", 0 },
  390. { },
  391. };
  392. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  393. static struct platform_driver sh_pfc_driver = {
  394. .probe = sh_pfc_probe,
  395. .remove = sh_pfc_remove,
  396. .id_table = sh_pfc_id_table,
  397. .driver = {
  398. .name = DRV_NAME,
  399. .owner = THIS_MODULE,
  400. },
  401. };
  402. static int __init sh_pfc_init(void)
  403. {
  404. return platform_driver_register(&sh_pfc_driver);
  405. }
  406. postcore_initcall(sh_pfc_init);
  407. static void __exit sh_pfc_exit(void)
  408. {
  409. platform_driver_unregister(&sh_pfc_driver);
  410. }
  411. module_exit(sh_pfc_exit);
  412. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  413. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  414. MODULE_LICENSE("GPL v2");