core.c 12 KB

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