core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. struct sh_pfc_pin *sh_pfc_get_pin(struct sh_pfc *pfc, unsigned int pin)
  63. {
  64. unsigned int offset;
  65. unsigned int i;
  66. if (pfc->info->ranges == NULL)
  67. return &pfc->info->pins[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. ? &pfc->info->pins[offset + pin - range->begin]
  73. : NULL;
  74. offset += range->end - range->begin + 1;
  75. }
  76. return NULL;
  77. }
  78. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  79. {
  80. if (enum_id < r->begin)
  81. return 0;
  82. if (enum_id > r->end)
  83. return 0;
  84. return 1;
  85. }
  86. unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  87. unsigned long reg_width)
  88. {
  89. switch (reg_width) {
  90. case 8:
  91. return ioread8(mapped_reg);
  92. case 16:
  93. return ioread16(mapped_reg);
  94. case 32:
  95. return ioread32(mapped_reg);
  96. }
  97. BUG();
  98. return 0;
  99. }
  100. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
  101. unsigned long data)
  102. {
  103. switch (reg_width) {
  104. case 8:
  105. iowrite8(data, mapped_reg);
  106. return;
  107. case 16:
  108. iowrite16(data, mapped_reg);
  109. return;
  110. case 32:
  111. iowrite32(data, mapped_reg);
  112. return;
  113. }
  114. BUG();
  115. }
  116. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  117. struct pinmux_cfg_reg *crp,
  118. unsigned long in_pos,
  119. void __iomem **mapped_regp,
  120. unsigned long *maskp,
  121. unsigned long *posp)
  122. {
  123. int k;
  124. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  125. if (crp->field_width) {
  126. *maskp = (1 << crp->field_width) - 1;
  127. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  128. } else {
  129. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  130. *posp = crp->reg_width;
  131. for (k = 0; k <= in_pos; k++)
  132. *posp -= crp->var_field_width[k];
  133. }
  134. }
  135. static int sh_pfc_read_config_reg(struct sh_pfc *pfc,
  136. struct pinmux_cfg_reg *crp,
  137. unsigned long field)
  138. {
  139. void __iomem *mapped_reg;
  140. unsigned long mask, pos;
  141. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  142. pr_debug("read_reg: addr = %lx, field = %ld, "
  143. "r_width = %ld, f_width = %ld\n",
  144. crp->reg, field, crp->reg_width, crp->field_width);
  145. return (sh_pfc_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  146. }
  147. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  148. struct pinmux_cfg_reg *crp,
  149. unsigned long field, unsigned long value)
  150. {
  151. void __iomem *mapped_reg;
  152. unsigned long mask, pos, data;
  153. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  154. pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
  155. "r_width = %ld, f_width = %ld\n",
  156. crp->reg, value, field, crp->reg_width, crp->field_width);
  157. mask = ~(mask << pos);
  158. value = value << pos;
  159. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  160. data &= mask;
  161. data |= value;
  162. if (pfc->info->unlock_reg)
  163. sh_pfc_write_raw_reg(
  164. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  165. ~data);
  166. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  167. }
  168. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  169. struct pinmux_cfg_reg **crp, int *fieldp,
  170. int *valuep, unsigned long **cntp)
  171. {
  172. struct pinmux_cfg_reg *config_reg;
  173. unsigned long r_width, f_width, curr_width, ncomb;
  174. int k, m, n, pos, bit_pos;
  175. k = 0;
  176. while (1) {
  177. config_reg = pfc->info->cfg_regs + k;
  178. r_width = config_reg->reg_width;
  179. f_width = config_reg->field_width;
  180. if (!r_width)
  181. break;
  182. pos = 0;
  183. m = 0;
  184. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  185. if (f_width)
  186. curr_width = f_width;
  187. else
  188. curr_width = config_reg->var_field_width[m];
  189. ncomb = 1 << curr_width;
  190. for (n = 0; n < ncomb; n++) {
  191. if (config_reg->enum_ids[pos + n] == enum_id) {
  192. *crp = config_reg;
  193. *fieldp = m;
  194. *valuep = n;
  195. *cntp = &config_reg->cnt[m];
  196. return 0;
  197. }
  198. }
  199. pos += ncomb;
  200. m++;
  201. }
  202. k++;
  203. }
  204. return -1;
  205. }
  206. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
  207. pinmux_enum_t *enum_idp)
  208. {
  209. pinmux_enum_t *data = pfc->info->gpio_data;
  210. int k;
  211. if (pos) {
  212. *enum_idp = data[pos + 1];
  213. return pos + 1;
  214. }
  215. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  216. if (data[k] == mark) {
  217. *enum_idp = data[k + 1];
  218. return k + 1;
  219. }
  220. }
  221. pr_err("cannot locate data/mark enum_id for mark %d\n", mark);
  222. return -1;
  223. }
  224. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type,
  225. int cfg_mode)
  226. {
  227. struct pinmux_cfg_reg *cr = NULL;
  228. pinmux_enum_t enum_id;
  229. struct pinmux_range *range;
  230. int in_range, pos, field, value;
  231. unsigned long *cntp;
  232. switch (pinmux_type) {
  233. case PINMUX_TYPE_FUNCTION:
  234. range = NULL;
  235. break;
  236. case PINMUX_TYPE_OUTPUT:
  237. range = &pfc->info->output;
  238. break;
  239. case PINMUX_TYPE_INPUT:
  240. range = &pfc->info->input;
  241. break;
  242. case PINMUX_TYPE_INPUT_PULLUP:
  243. range = &pfc->info->input_pu;
  244. break;
  245. case PINMUX_TYPE_INPUT_PULLDOWN:
  246. range = &pfc->info->input_pd;
  247. break;
  248. default:
  249. goto out_err;
  250. }
  251. pos = 0;
  252. enum_id = 0;
  253. field = 0;
  254. value = 0;
  255. while (1) {
  256. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  257. if (pos <= 0)
  258. goto out_err;
  259. if (!enum_id)
  260. break;
  261. /* first check if this is a function enum */
  262. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  263. if (!in_range) {
  264. /* not a function enum */
  265. if (range) {
  266. /*
  267. * other range exists, so this pin is
  268. * a regular GPIO pin that now is being
  269. * bound to a specific direction.
  270. *
  271. * for this case we only allow function enums
  272. * and the enums that match the other range.
  273. */
  274. in_range = sh_pfc_enum_in_range(enum_id, range);
  275. /*
  276. * special case pass through for fixed
  277. * input-only or output-only pins without
  278. * function enum register association.
  279. */
  280. if (in_range && enum_id == range->force)
  281. continue;
  282. } else {
  283. /*
  284. * no other range exists, so this pin
  285. * must then be of the function type.
  286. *
  287. * allow function type pins to select
  288. * any combination of function/in/out
  289. * in their MARK lists.
  290. */
  291. in_range = 1;
  292. }
  293. }
  294. if (!in_range)
  295. continue;
  296. if (sh_pfc_get_config_reg(pfc, enum_id, &cr,
  297. &field, &value, &cntp) != 0)
  298. goto out_err;
  299. switch (cfg_mode) {
  300. case GPIO_CFG_DRYRUN:
  301. if (!*cntp ||
  302. (sh_pfc_read_config_reg(pfc, cr, field) != value))
  303. continue;
  304. break;
  305. case GPIO_CFG_REQ:
  306. sh_pfc_write_config_reg(pfc, cr, field, value);
  307. *cntp = *cntp + 1;
  308. break;
  309. case GPIO_CFG_FREE:
  310. *cntp = *cntp - 1;
  311. break;
  312. }
  313. }
  314. return 0;
  315. out_err:
  316. return -1;
  317. }
  318. static int sh_pfc_probe(struct platform_device *pdev)
  319. {
  320. struct sh_pfc_soc_info *info;
  321. struct sh_pfc *pfc;
  322. int ret;
  323. /*
  324. * Ensure that the type encoding fits
  325. */
  326. BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
  327. info = pdev->id_entry->driver_data
  328. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  329. if (info == NULL)
  330. return -ENODEV;
  331. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  332. if (pfc == NULL)
  333. return -ENOMEM;
  334. pfc->info = info;
  335. pfc->dev = &pdev->dev;
  336. ret = sh_pfc_ioremap(pfc, pdev);
  337. if (unlikely(ret < 0))
  338. return ret;
  339. spin_lock_init(&pfc->lock);
  340. pinctrl_provide_dummies();
  341. /*
  342. * Initialize pinctrl bindings first
  343. */
  344. ret = sh_pfc_register_pinctrl(pfc);
  345. if (unlikely(ret != 0))
  346. return ret;
  347. #ifdef CONFIG_GPIO_SH_PFC
  348. /*
  349. * Then the GPIO chip
  350. */
  351. ret = sh_pfc_register_gpiochip(pfc);
  352. if (unlikely(ret != 0)) {
  353. /*
  354. * If the GPIO chip fails to come up we still leave the
  355. * PFC state as it is, given that there are already
  356. * extant users of it that have succeeded by this point.
  357. */
  358. pr_notice("failed to init GPIO chip, ignoring...\n");
  359. }
  360. #endif
  361. platform_set_drvdata(pdev, pfc);
  362. pr_info("%s support registered\n", info->name);
  363. return 0;
  364. }
  365. static int sh_pfc_remove(struct platform_device *pdev)
  366. {
  367. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  368. #ifdef CONFIG_GPIO_SH_PFC
  369. sh_pfc_unregister_gpiochip(pfc);
  370. #endif
  371. sh_pfc_unregister_pinctrl(pfc);
  372. platform_set_drvdata(pdev, NULL);
  373. return 0;
  374. }
  375. static const struct platform_device_id sh_pfc_id_table[] = {
  376. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  377. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  378. #endif
  379. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  380. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  381. #endif
  382. #ifdef CONFIG_PINCTRL_PFC_SH7203
  383. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  384. #endif
  385. #ifdef CONFIG_PINCTRL_PFC_SH7264
  386. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  387. #endif
  388. #ifdef CONFIG_PINCTRL_PFC_SH7269
  389. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  390. #endif
  391. #ifdef CONFIG_PINCTRL_PFC_SH7372
  392. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  393. #endif
  394. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  395. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  396. #endif
  397. #ifdef CONFIG_PINCTRL_PFC_SH7720
  398. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  399. #endif
  400. #ifdef CONFIG_PINCTRL_PFC_SH7722
  401. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  402. #endif
  403. #ifdef CONFIG_PINCTRL_PFC_SH7723
  404. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  405. #endif
  406. #ifdef CONFIG_PINCTRL_PFC_SH7724
  407. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  408. #endif
  409. #ifdef CONFIG_PINCTRL_PFC_SH7734
  410. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  411. #endif
  412. #ifdef CONFIG_PINCTRL_PFC_SH7757
  413. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  414. #endif
  415. #ifdef CONFIG_PINCTRL_PFC_SH7785
  416. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  417. #endif
  418. #ifdef CONFIG_PINCTRL_PFC_SH7786
  419. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  420. #endif
  421. #ifdef CONFIG_PINCTRL_PFC_SHX3
  422. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  423. #endif
  424. { "sh-pfc", 0 },
  425. { },
  426. };
  427. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  428. static struct platform_driver sh_pfc_driver = {
  429. .probe = sh_pfc_probe,
  430. .remove = sh_pfc_remove,
  431. .id_table = sh_pfc_id_table,
  432. .driver = {
  433. .name = DRV_NAME,
  434. .owner = THIS_MODULE,
  435. },
  436. };
  437. static int __init sh_pfc_init(void)
  438. {
  439. return platform_driver_register(&sh_pfc_driver);
  440. }
  441. postcore_initcall(sh_pfc_init);
  442. static void __exit sh_pfc_exit(void)
  443. {
  444. platform_driver_unregister(&sh_pfc_driver);
  445. }
  446. module_exit(sh_pfc_exit);
  447. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  448. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  449. MODULE_LICENSE("GPL v2");