core.c 12 KB

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