sh_pfc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Pinmuxed GPIO support for SuperH.
  3. * Copy from linux kernel driver/sh/pfc.c
  4. *
  5. * Copyright (C) 2008 Magnus Damm
  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. #include <common.h>
  12. #include <asm/bitops.h>
  13. #include <asm/io.h>
  14. #include <sh_pfc.h>
  15. static struct pinmux_info *gpioc;
  16. #define pfc_phys_to_virt(p, a) ((void *)a)
  17. static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  18. {
  19. if (enum_id < r->begin)
  20. return 0;
  21. if (enum_id > r->end)
  22. return 0;
  23. return 1;
  24. }
  25. static unsigned long gpio_read_raw_reg(void *mapped_reg,
  26. unsigned long reg_width)
  27. {
  28. switch (reg_width) {
  29. case 8:
  30. return readb(mapped_reg);
  31. case 16:
  32. return readw(mapped_reg);
  33. case 32:
  34. return readl(mapped_reg);
  35. }
  36. BUG();
  37. return 0;
  38. }
  39. static void gpio_write_raw_reg(void *mapped_reg,
  40. unsigned long reg_width,
  41. unsigned long data)
  42. {
  43. switch (reg_width) {
  44. case 8:
  45. writeb(data, mapped_reg);
  46. return;
  47. case 16:
  48. writew(data, mapped_reg);
  49. return;
  50. case 32:
  51. writel(data, mapped_reg);
  52. return;
  53. }
  54. BUG();
  55. }
  56. static int gpio_read_bit(struct pinmux_data_reg *dr,
  57. unsigned long offset,
  58. unsigned long in_pos)
  59. {
  60. unsigned long pos;
  61. pos = dr->reg_width - (in_pos + 1);
  62. debug("read_bit: addr = %lx, pos = %ld, r_width = %ld\n",
  63. dr->reg + offset, pos, dr->reg_width);
  64. return (gpio_read_raw_reg(dr->mapped_reg + offset,
  65. dr->reg_width) >> pos) & 1;
  66. }
  67. static void gpio_write_bit(struct pinmux_data_reg *dr,
  68. unsigned long in_pos, unsigned long value)
  69. {
  70. unsigned long pos;
  71. pos = dr->reg_width - (in_pos + 1);
  72. debug("write_bit addr = %lx, value = %d, pos = %ld, "
  73. "r_width = %ld\n",
  74. dr->reg, !!value, pos, dr->reg_width);
  75. if (value)
  76. __set_bit(pos, &dr->reg_shadow);
  77. else
  78. __clear_bit(pos, &dr->reg_shadow);
  79. gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
  80. }
  81. static void config_reg_helper(struct pinmux_info *gpioc,
  82. struct pinmux_cfg_reg *crp,
  83. unsigned long in_pos,
  84. #if 0
  85. void __iomem **mapped_regp,
  86. #else
  87. void **mapped_regp,
  88. #endif
  89. unsigned long *maskp,
  90. unsigned long *posp)
  91. {
  92. int k;
  93. *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
  94. if (crp->field_width) {
  95. *maskp = (1 << crp->field_width) - 1;
  96. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  97. } else {
  98. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  99. *posp = crp->reg_width;
  100. for (k = 0; k <= in_pos; k++)
  101. *posp -= crp->var_field_width[k];
  102. }
  103. }
  104. static int read_config_reg(struct pinmux_info *gpioc,
  105. struct pinmux_cfg_reg *crp,
  106. unsigned long field)
  107. {
  108. void *mapped_reg;
  109. unsigned long mask, pos;
  110. config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
  111. debug("read_reg: addr = %lx, field = %ld, "
  112. "r_width = %ld, f_width = %ld\n",
  113. crp->reg, field, crp->reg_width, crp->field_width);
  114. return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  115. }
  116. static void write_config_reg(struct pinmux_info *gpioc,
  117. struct pinmux_cfg_reg *crp,
  118. unsigned long field, unsigned long value)
  119. {
  120. void *mapped_reg;
  121. unsigned long mask, pos, data;
  122. config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
  123. debug("write_reg addr = %lx, value = %ld, field = %ld, "
  124. "r_width = %ld, f_width = %ld\n",
  125. crp->reg, value, field, crp->reg_width, crp->field_width);
  126. mask = ~(mask << pos);
  127. value = value << pos;
  128. data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
  129. data &= mask;
  130. data |= value;
  131. if (gpioc->unlock_reg)
  132. gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
  133. 32, ~data);
  134. gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
  135. }
  136. static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
  137. {
  138. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  139. struct pinmux_data_reg *data_reg;
  140. int k, n;
  141. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  142. return -1;
  143. k = 0;
  144. while (1) {
  145. data_reg = gpioc->data_regs + k;
  146. if (!data_reg->reg_width)
  147. break;
  148. data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
  149. for (n = 0; n < data_reg->reg_width; n++) {
  150. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  151. gpiop->flags &= ~PINMUX_FLAG_DREG;
  152. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  153. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  154. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  155. return 0;
  156. }
  157. }
  158. k++;
  159. }
  160. BUG();
  161. return -1;
  162. }
  163. static void setup_data_regs(struct pinmux_info *gpioc)
  164. {
  165. struct pinmux_data_reg *drp;
  166. int k;
  167. for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
  168. setup_data_reg(gpioc, k);
  169. k = 0;
  170. while (1) {
  171. drp = gpioc->data_regs + k;
  172. if (!drp->reg_width)
  173. break;
  174. drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
  175. drp->reg_width);
  176. k++;
  177. }
  178. }
  179. static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
  180. struct pinmux_data_reg **drp, int *bitp)
  181. {
  182. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  183. int k, n;
  184. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  185. return -1;
  186. k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
  187. n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  188. *drp = gpioc->data_regs + k;
  189. *bitp = n;
  190. return 0;
  191. }
  192. static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
  193. struct pinmux_cfg_reg **crp,
  194. int *fieldp, int *valuep,
  195. unsigned long **cntp)
  196. {
  197. struct pinmux_cfg_reg *config_reg;
  198. unsigned long r_width, f_width, curr_width, ncomb;
  199. int k, m, n, pos, bit_pos;
  200. k = 0;
  201. while (1) {
  202. config_reg = gpioc->cfg_regs + k;
  203. r_width = config_reg->reg_width;
  204. f_width = config_reg->field_width;
  205. if (!r_width)
  206. break;
  207. pos = 0;
  208. m = 0;
  209. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  210. if (f_width)
  211. curr_width = f_width;
  212. else
  213. curr_width = config_reg->var_field_width[m];
  214. ncomb = 1 << curr_width;
  215. for (n = 0; n < ncomb; n++) {
  216. if (config_reg->enum_ids[pos + n] == enum_id) {
  217. *crp = config_reg;
  218. *fieldp = m;
  219. *valuep = n;
  220. *cntp = &config_reg->cnt[m];
  221. return 0;
  222. }
  223. }
  224. pos += ncomb;
  225. m++;
  226. }
  227. k++;
  228. }
  229. return -1;
  230. }
  231. static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
  232. int pos, pinmux_enum_t *enum_idp)
  233. {
  234. pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
  235. pinmux_enum_t *data = gpioc->gpio_data;
  236. int k;
  237. if (!enum_in_range(enum_id, &gpioc->data)) {
  238. if (!enum_in_range(enum_id, &gpioc->mark)) {
  239. debug("non data/mark enum_id for gpio %d\n", gpio);
  240. return -1;
  241. }
  242. }
  243. if (pos) {
  244. *enum_idp = data[pos + 1];
  245. return pos + 1;
  246. }
  247. for (k = 0; k < gpioc->gpio_data_size; k++) {
  248. if (data[k] == enum_id) {
  249. *enum_idp = data[k + 1];
  250. return k + 1;
  251. }
  252. }
  253. debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
  254. return -1;
  255. }
  256. enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
  257. static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
  258. int pinmux_type, int cfg_mode)
  259. {
  260. struct pinmux_cfg_reg *cr = NULL;
  261. pinmux_enum_t enum_id;
  262. struct pinmux_range *range;
  263. int in_range, pos, field, value;
  264. unsigned long *cntp;
  265. switch (pinmux_type) {
  266. case PINMUX_TYPE_FUNCTION:
  267. range = NULL;
  268. break;
  269. case PINMUX_TYPE_OUTPUT:
  270. range = &gpioc->output;
  271. break;
  272. case PINMUX_TYPE_INPUT:
  273. range = &gpioc->input;
  274. break;
  275. case PINMUX_TYPE_INPUT_PULLUP:
  276. range = &gpioc->input_pu;
  277. break;
  278. case PINMUX_TYPE_INPUT_PULLDOWN:
  279. range = &gpioc->input_pd;
  280. break;
  281. default:
  282. goto out_err;
  283. }
  284. pos = 0;
  285. enum_id = 0;
  286. field = 0;
  287. value = 0;
  288. while (1) {
  289. pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
  290. if (pos <= 0)
  291. goto out_err;
  292. if (!enum_id)
  293. break;
  294. /* first check if this is a function enum */
  295. in_range = enum_in_range(enum_id, &gpioc->function);
  296. if (!in_range) {
  297. /* not a function enum */
  298. if (range) {
  299. /*
  300. * other range exists, so this pin is
  301. * a regular GPIO pin that now is being
  302. * bound to a specific direction.
  303. *
  304. * for this case we only allow function enums
  305. * and the enums that match the other range.
  306. */
  307. in_range = enum_in_range(enum_id, range);
  308. /*
  309. * special case pass through for fixed
  310. * input-only or output-only pins without
  311. * function enum register association.
  312. */
  313. if (in_range && enum_id == range->force)
  314. continue;
  315. } else {
  316. /*
  317. * no other range exists, so this pin
  318. * must then be of the function type.
  319. *
  320. * allow function type pins to select
  321. * any combination of function/in/out
  322. * in their MARK lists.
  323. */
  324. in_range = 1;
  325. }
  326. }
  327. if (!in_range)
  328. continue;
  329. if (get_config_reg(gpioc, enum_id, &cr,
  330. &field, &value, &cntp) != 0)
  331. goto out_err;
  332. switch (cfg_mode) {
  333. case GPIO_CFG_DRYRUN:
  334. if (!*cntp ||
  335. (read_config_reg(gpioc, cr, field) != value))
  336. continue;
  337. break;
  338. case GPIO_CFG_REQ:
  339. write_config_reg(gpioc, cr, field, value);
  340. *cntp = *cntp + 1;
  341. break;
  342. case GPIO_CFG_FREE:
  343. *cntp = *cntp - 1;
  344. break;
  345. }
  346. }
  347. return 0;
  348. out_err:
  349. return -1;
  350. }
  351. #if 0
  352. static DEFINE_SPINLOCK(gpio_lock);
  353. static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
  354. {
  355. return container_of(chip, struct pinmux_info, chip);
  356. }
  357. #endif
  358. static int sh_gpio_request(unsigned offset)
  359. {
  360. struct pinmux_data_reg *dummy;
  361. int i, ret, pinmux_type;
  362. ret = -1;
  363. if (!gpioc)
  364. goto err_out;
  365. if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
  366. goto err_out;
  367. /* setup pin function here if no data is associated with pin */
  368. if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
  369. pinmux_type = PINMUX_TYPE_FUNCTION;
  370. else
  371. pinmux_type = PINMUX_TYPE_GPIO;
  372. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  373. if (pinmux_config_gpio(gpioc, offset,
  374. pinmux_type,
  375. GPIO_CFG_DRYRUN) != 0)
  376. goto err_out;
  377. if (pinmux_config_gpio(gpioc, offset,
  378. pinmux_type,
  379. GPIO_CFG_REQ) != 0)
  380. BUG();
  381. }
  382. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  383. gpioc->gpios[offset].flags |= pinmux_type;
  384. ret = 0;
  385. err_out:
  386. return ret;
  387. }
  388. static void sh_gpio_free(unsigned offset)
  389. {
  390. int pinmux_type;
  391. if (!gpioc)
  392. return;
  393. pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  394. pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
  395. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  396. gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
  397. }
  398. static int pinmux_direction(struct pinmux_info *gpioc,
  399. unsigned gpio, int new_pinmux_type)
  400. {
  401. int pinmux_type;
  402. int ret = -1;
  403. if (!gpioc)
  404. goto err_out;
  405. pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
  406. switch (pinmux_type) {
  407. case PINMUX_TYPE_GPIO:
  408. break;
  409. case PINMUX_TYPE_OUTPUT:
  410. case PINMUX_TYPE_INPUT:
  411. case PINMUX_TYPE_INPUT_PULLUP:
  412. case PINMUX_TYPE_INPUT_PULLDOWN:
  413. pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
  414. break;
  415. default:
  416. goto err_out;
  417. }
  418. if (pinmux_config_gpio(gpioc, gpio,
  419. new_pinmux_type,
  420. GPIO_CFG_DRYRUN) != 0)
  421. goto err_out;
  422. if (pinmux_config_gpio(gpioc, gpio,
  423. new_pinmux_type,
  424. GPIO_CFG_REQ) != 0)
  425. BUG();
  426. gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
  427. gpioc->gpios[gpio].flags |= new_pinmux_type;
  428. ret = 0;
  429. err_out:
  430. return ret;
  431. }
  432. static int sh_gpio_direction_input(unsigned offset)
  433. {
  434. return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
  435. }
  436. static void sh_gpio_set_value(struct pinmux_info *gpioc,
  437. unsigned gpio, int value)
  438. {
  439. struct pinmux_data_reg *dr = NULL;
  440. int bit = 0;
  441. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  442. BUG();
  443. else
  444. gpio_write_bit(dr, bit, value);
  445. }
  446. static int sh_gpio_direction_output(unsigned offset, int value)
  447. {
  448. sh_gpio_set_value(gpioc, offset, value);
  449. return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
  450. }
  451. static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
  452. {
  453. struct pinmux_data_reg *dr = NULL;
  454. int bit = 0, offset = 0;
  455. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  456. return -1;
  457. #if defined(CONFIG_RCAR_GEN3)
  458. if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_INPUT)
  459. offset += 4;
  460. #endif
  461. return gpio_read_bit(dr, offset, bit);
  462. }
  463. static int sh_gpio_get(unsigned offset)
  464. {
  465. return sh_gpio_get_value(gpioc, offset);
  466. }
  467. static void sh_gpio_set(unsigned offset, int value)
  468. {
  469. sh_gpio_set_value(gpioc, offset, value);
  470. }
  471. int register_pinmux(struct pinmux_info *pip)
  472. {
  473. if (pip != NULL) {
  474. gpioc = pip;
  475. debug("%s deregistering\n", pip->name);
  476. setup_data_regs(gpioc);
  477. }
  478. return 0;
  479. }
  480. int unregister_pinmux(struct pinmux_info *pip)
  481. {
  482. debug("%s deregistering\n", pip->name);
  483. if (gpioc != pip)
  484. return -1;
  485. gpioc = NULL;
  486. return 0;
  487. }
  488. int gpio_request(unsigned gpio, const char *label)
  489. {
  490. sh_gpio_request(gpio);
  491. return 0;
  492. }
  493. int gpio_free(unsigned gpio)
  494. {
  495. sh_gpio_free(gpio);
  496. return 0;
  497. }
  498. int gpio_direction_input(unsigned gpio)
  499. {
  500. return sh_gpio_direction_input(gpio);
  501. }
  502. int gpio_direction_output(unsigned gpio, int value)
  503. {
  504. return sh_gpio_direction_output(gpio, value);
  505. }
  506. void gpio_set_value(unsigned gpio, int value)
  507. {
  508. sh_gpio_set(gpio, value);
  509. }
  510. int gpio_get_value(unsigned gpio)
  511. {
  512. return sh_gpio_get(gpio);
  513. }