core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #include <linux/bitops.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pinctrl/machine.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "core.h"
  23. static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
  24. {
  25. struct resource *res;
  26. int k;
  27. if (pdev->num_resources == 0)
  28. return -EINVAL;
  29. pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
  30. sizeof(*pfc->window), GFP_NOWAIT);
  31. if (!pfc->window)
  32. return -ENOMEM;
  33. pfc->num_windows = pdev->num_resources;
  34. for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
  35. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  36. pfc->window[k].phys = res->start;
  37. pfc->window[k].size = resource_size(res);
  38. pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
  39. resource_size(res));
  40. if (!pfc->window[k].virt)
  41. return -ENOMEM;
  42. }
  43. return 0;
  44. }
  45. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  46. unsigned long address)
  47. {
  48. struct sh_pfc_window *window;
  49. unsigned int i;
  50. /* scan through physical windows and convert address */
  51. for (i = 0; i < pfc->num_windows; i++) {
  52. window = pfc->window + i;
  53. if (address < window->phys)
  54. continue;
  55. if (address >= (window->phys + window->size))
  56. continue;
  57. return window->virt + (address - window->phys);
  58. }
  59. BUG();
  60. }
  61. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  62. {
  63. unsigned int offset;
  64. unsigned int i;
  65. if (pfc->info->ranges == NULL)
  66. return pin;
  67. for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
  68. const struct pinmux_range *range = &pfc->info->ranges[i];
  69. if (pin <= range->end)
  70. return pin >= range->begin
  71. ? offset + pin - range->begin : -1;
  72. offset += range->end - range->begin + 1;
  73. }
  74. return -EINVAL;
  75. }
  76. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id,
  77. const 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. const 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. const 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. dev_dbg(pfc->dev, "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. const struct pinmux_cfg_reg **crp, int *fieldp,
  157. int *valuep)
  158. {
  159. const 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 -EINVAL;
  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. const 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. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  208. mark);
  209. return -EINVAL;
  210. }
  211. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  212. {
  213. const struct pinmux_cfg_reg *cr = NULL;
  214. pinmux_enum_t enum_id;
  215. const struct pinmux_range *range;
  216. int in_range, pos, field, value;
  217. int ret;
  218. switch (pinmux_type) {
  219. case PINMUX_TYPE_FUNCTION:
  220. range = NULL;
  221. break;
  222. case PINMUX_TYPE_OUTPUT:
  223. range = &pfc->info->output;
  224. break;
  225. case PINMUX_TYPE_INPUT:
  226. range = &pfc->info->input;
  227. break;
  228. case PINMUX_TYPE_INPUT_PULLUP:
  229. range = &pfc->info->input_pu;
  230. break;
  231. case PINMUX_TYPE_INPUT_PULLDOWN:
  232. range = &pfc->info->input_pd;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. pos = 0;
  238. enum_id = 0;
  239. field = 0;
  240. value = 0;
  241. while (1) {
  242. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  243. if (pos < 0)
  244. return pos;
  245. if (!enum_id)
  246. break;
  247. /* first check if this is a function enum */
  248. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  249. if (!in_range) {
  250. /* not a function enum */
  251. if (range) {
  252. /*
  253. * other range exists, so this pin is
  254. * a regular GPIO pin that now is being
  255. * bound to a specific direction.
  256. *
  257. * for this case we only allow function enums
  258. * and the enums that match the other range.
  259. */
  260. in_range = sh_pfc_enum_in_range(enum_id, range);
  261. /*
  262. * special case pass through for fixed
  263. * input-only or output-only pins without
  264. * function enum register association.
  265. */
  266. if (in_range && enum_id == range->force)
  267. continue;
  268. } else {
  269. /*
  270. * no other range exists, so this pin
  271. * must then be of the function type.
  272. *
  273. * allow function type pins to select
  274. * any combination of function/in/out
  275. * in their MARK lists.
  276. */
  277. in_range = 1;
  278. }
  279. }
  280. if (!in_range)
  281. continue;
  282. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  283. if (ret < 0)
  284. return ret;
  285. sh_pfc_write_config_reg(pfc, cr, field, value);
  286. }
  287. return 0;
  288. }
  289. static int sh_pfc_probe(struct platform_device *pdev)
  290. {
  291. const struct sh_pfc_soc_info *info;
  292. struct sh_pfc *pfc;
  293. int ret;
  294. info = pdev->id_entry->driver_data
  295. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  296. if (info == NULL)
  297. return -ENODEV;
  298. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  299. if (pfc == NULL)
  300. return -ENOMEM;
  301. pfc->info = info;
  302. pfc->dev = &pdev->dev;
  303. ret = sh_pfc_ioremap(pfc, pdev);
  304. if (unlikely(ret < 0))
  305. return ret;
  306. spin_lock_init(&pfc->lock);
  307. pinctrl_provide_dummies();
  308. /*
  309. * Initialize pinctrl bindings first
  310. */
  311. ret = sh_pfc_register_pinctrl(pfc);
  312. if (unlikely(ret != 0))
  313. return ret;
  314. #ifdef CONFIG_GPIO_SH_PFC
  315. /*
  316. * Then the GPIO chip
  317. */
  318. ret = sh_pfc_register_gpiochip(pfc);
  319. if (unlikely(ret != 0)) {
  320. /*
  321. * If the GPIO chip fails to come up we still leave the
  322. * PFC state as it is, given that there are already
  323. * extant users of it that have succeeded by this point.
  324. */
  325. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  326. }
  327. #endif
  328. platform_set_drvdata(pdev, pfc);
  329. dev_info(pfc->dev, "%s support registered\n", info->name);
  330. return 0;
  331. }
  332. static int sh_pfc_remove(struct platform_device *pdev)
  333. {
  334. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  335. #ifdef CONFIG_GPIO_SH_PFC
  336. sh_pfc_unregister_gpiochip(pfc);
  337. #endif
  338. sh_pfc_unregister_pinctrl(pfc);
  339. platform_set_drvdata(pdev, NULL);
  340. return 0;
  341. }
  342. static const struct platform_device_id sh_pfc_id_table[] = {
  343. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  344. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  345. #endif
  346. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  347. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  348. #endif
  349. #ifdef CONFIG_PINCTRL_PFC_SH7203
  350. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  351. #endif
  352. #ifdef CONFIG_PINCTRL_PFC_SH7264
  353. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  354. #endif
  355. #ifdef CONFIG_PINCTRL_PFC_SH7269
  356. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  357. #endif
  358. #ifdef CONFIG_PINCTRL_PFC_SH7372
  359. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  360. #endif
  361. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  362. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  363. #endif
  364. #ifdef CONFIG_PINCTRL_PFC_SH7720
  365. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  366. #endif
  367. #ifdef CONFIG_PINCTRL_PFC_SH7722
  368. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  369. #endif
  370. #ifdef CONFIG_PINCTRL_PFC_SH7723
  371. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  372. #endif
  373. #ifdef CONFIG_PINCTRL_PFC_SH7724
  374. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  375. #endif
  376. #ifdef CONFIG_PINCTRL_PFC_SH7734
  377. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  378. #endif
  379. #ifdef CONFIG_PINCTRL_PFC_SH7757
  380. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  381. #endif
  382. #ifdef CONFIG_PINCTRL_PFC_SH7785
  383. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  384. #endif
  385. #ifdef CONFIG_PINCTRL_PFC_SH7786
  386. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  387. #endif
  388. #ifdef CONFIG_PINCTRL_PFC_SHX3
  389. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  390. #endif
  391. { "sh-pfc", 0 },
  392. { },
  393. };
  394. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  395. static struct platform_driver sh_pfc_driver = {
  396. .probe = sh_pfc_probe,
  397. .remove = sh_pfc_remove,
  398. .id_table = sh_pfc_id_table,
  399. .driver = {
  400. .name = DRV_NAME,
  401. .owner = THIS_MODULE,
  402. },
  403. };
  404. static int __init sh_pfc_init(void)
  405. {
  406. return platform_driver_register(&sh_pfc_driver);
  407. }
  408. postcore_initcall(sh_pfc_init);
  409. static void __exit sh_pfc_exit(void)
  410. {
  411. platform_driver_unregister(&sh_pfc_driver);
  412. }
  413. module_exit(sh_pfc_exit);
  414. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  415. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  416. MODULE_LICENSE("GPL v2");