core.c 13 KB

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