core.c 14 KB

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