gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * arch/arm/plat-orion/gpio.c
  3. *
  4. * Marvell Orion SoC GPIO handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/bitops.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. /*
  19. * GPIO unit register offsets.
  20. */
  21. #define GPIO_OUT_OFF 0x0000
  22. #define GPIO_IO_CONF_OFF 0x0004
  23. #define GPIO_BLINK_EN_OFF 0x0008
  24. #define GPIO_IN_POL_OFF 0x000c
  25. #define GPIO_DATA_IN_OFF 0x0010
  26. #define GPIO_EDGE_CAUSE_OFF 0x0014
  27. #define GPIO_EDGE_MASK_OFF 0x0018
  28. #define GPIO_LEVEL_MASK_OFF 0x001c
  29. struct orion_gpio_chip {
  30. struct gpio_chip chip;
  31. spinlock_t lock;
  32. void __iomem *base;
  33. unsigned long valid_input;
  34. unsigned long valid_output;
  35. int mask_offset;
  36. int secondary_irq_base;
  37. };
  38. static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
  39. {
  40. return ochip->base + GPIO_OUT_OFF;
  41. }
  42. static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
  43. {
  44. return ochip->base + GPIO_IO_CONF_OFF;
  45. }
  46. static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
  47. {
  48. return ochip->base + GPIO_BLINK_EN_OFF;
  49. }
  50. static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
  51. {
  52. return ochip->base + GPIO_IN_POL_OFF;
  53. }
  54. static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
  55. {
  56. return ochip->base + GPIO_DATA_IN_OFF;
  57. }
  58. static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
  59. {
  60. return ochip->base + GPIO_EDGE_CAUSE_OFF;
  61. }
  62. static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
  63. {
  64. return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
  65. }
  66. static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
  67. {
  68. return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
  69. }
  70. static struct orion_gpio_chip orion_gpio_chips[2];
  71. static int orion_gpio_chip_count;
  72. static inline void
  73. __set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
  74. {
  75. u32 u;
  76. u = readl(GPIO_IO_CONF(ochip));
  77. if (input)
  78. u |= 1 << pin;
  79. else
  80. u &= ~(1 << pin);
  81. writel(u, GPIO_IO_CONF(ochip));
  82. }
  83. static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
  84. {
  85. u32 u;
  86. u = readl(GPIO_OUT(ochip));
  87. if (high)
  88. u |= 1 << pin;
  89. else
  90. u &= ~(1 << pin);
  91. writel(u, GPIO_OUT(ochip));
  92. }
  93. static inline void
  94. __set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
  95. {
  96. u32 u;
  97. u = readl(GPIO_BLINK_EN(ochip));
  98. if (blink)
  99. u |= 1 << pin;
  100. else
  101. u &= ~(1 << pin);
  102. writel(u, GPIO_BLINK_EN(ochip));
  103. }
  104. static inline int
  105. orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
  106. {
  107. if (pin >= ochip->chip.ngpio)
  108. goto err_out;
  109. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
  110. goto err_out;
  111. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
  112. goto err_out;
  113. return 1;
  114. err_out:
  115. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  116. return false;
  117. }
  118. /*
  119. * GENERIC_GPIO primitives.
  120. */
  121. static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
  122. {
  123. struct orion_gpio_chip *ochip =
  124. container_of(chip, struct orion_gpio_chip, chip);
  125. if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
  126. orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  127. return 0;
  128. return -EINVAL;
  129. }
  130. static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  131. {
  132. struct orion_gpio_chip *ochip =
  133. container_of(chip, struct orion_gpio_chip, chip);
  134. unsigned long flags;
  135. if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
  136. return -EINVAL;
  137. spin_lock_irqsave(&ochip->lock, flags);
  138. __set_direction(ochip, pin, 1);
  139. spin_unlock_irqrestore(&ochip->lock, flags);
  140. return 0;
  141. }
  142. static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
  143. {
  144. struct orion_gpio_chip *ochip =
  145. container_of(chip, struct orion_gpio_chip, chip);
  146. int val;
  147. if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
  148. val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
  149. } else {
  150. val = readl(GPIO_OUT(ochip));
  151. }
  152. return (val >> pin) & 1;
  153. }
  154. static int
  155. orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
  156. {
  157. struct orion_gpio_chip *ochip =
  158. container_of(chip, struct orion_gpio_chip, chip);
  159. unsigned long flags;
  160. if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  161. return -EINVAL;
  162. spin_lock_irqsave(&ochip->lock, flags);
  163. __set_blinking(ochip, pin, 0);
  164. __set_level(ochip, pin, value);
  165. __set_direction(ochip, pin, 0);
  166. spin_unlock_irqrestore(&ochip->lock, flags);
  167. return 0;
  168. }
  169. static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  170. {
  171. struct orion_gpio_chip *ochip =
  172. container_of(chip, struct orion_gpio_chip, chip);
  173. unsigned long flags;
  174. spin_lock_irqsave(&ochip->lock, flags);
  175. __set_level(ochip, pin, value);
  176. spin_unlock_irqrestore(&ochip->lock, flags);
  177. }
  178. static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  179. {
  180. struct orion_gpio_chip *ochip =
  181. container_of(chip, struct orion_gpio_chip, chip);
  182. return ochip->secondary_irq_base + pin;
  183. }
  184. /*
  185. * Orion-specific GPIO API extensions.
  186. */
  187. static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
  188. {
  189. int i;
  190. for (i = 0; i < orion_gpio_chip_count; i++) {
  191. struct orion_gpio_chip *ochip = orion_gpio_chips + i;
  192. struct gpio_chip *chip = &ochip->chip;
  193. if (pin >= chip->base && pin < chip->base + chip->ngpio)
  194. return ochip;
  195. }
  196. return NULL;
  197. }
  198. void __init orion_gpio_set_unused(unsigned pin)
  199. {
  200. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  201. if (ochip == NULL)
  202. return;
  203. pin -= ochip->chip.base;
  204. /* Configure as output, drive low. */
  205. __set_level(ochip, pin, 0);
  206. __set_direction(ochip, pin, 0);
  207. }
  208. void __init orion_gpio_set_valid(unsigned pin, int mode)
  209. {
  210. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  211. if (ochip == NULL)
  212. return;
  213. pin -= ochip->chip.base;
  214. if (mode == 1)
  215. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  216. if (mode & GPIO_INPUT_OK)
  217. __set_bit(pin, &ochip->valid_input);
  218. else
  219. __clear_bit(pin, &ochip->valid_input);
  220. if (mode & GPIO_OUTPUT_OK)
  221. __set_bit(pin, &ochip->valid_output);
  222. else
  223. __clear_bit(pin, &ochip->valid_output);
  224. }
  225. void orion_gpio_set_blink(unsigned pin, int blink)
  226. {
  227. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  228. unsigned long flags;
  229. if (ochip == NULL)
  230. return;
  231. spin_lock_irqsave(&ochip->lock, flags);
  232. __set_level(ochip, pin, 0);
  233. __set_blinking(ochip, pin, blink);
  234. spin_unlock_irqrestore(&ochip->lock, flags);
  235. }
  236. EXPORT_SYMBOL(orion_gpio_set_blink);
  237. /*****************************************************************************
  238. * Orion GPIO IRQ
  239. *
  240. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  241. * value of the line or the opposite value.
  242. *
  243. * Level IRQ handlers: DATA_IN is used directly as cause register.
  244. * Interrupt are masked by LEVEL_MASK registers.
  245. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  246. * Interrupt are masked by EDGE_MASK registers.
  247. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  248. * the polarity to catch the next line transaction.
  249. * This is a race condition that might not perfectly
  250. * work on some use cases.
  251. *
  252. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  253. * cause register.
  254. *
  255. * EDGE cause mask
  256. * data-in /--------| |-----| |----\
  257. * -----| |----- ---- to main cause reg
  258. * X \----------------| |----/
  259. * polarity LEVEL mask
  260. *
  261. ****************************************************************************/
  262. static void gpio_irq_ack(struct irq_data *d)
  263. {
  264. struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
  265. int type = irqd_get_trigger_type(d);
  266. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  267. int pin = d->irq - ochip->secondary_irq_base;
  268. writel(~(1 << pin), GPIO_EDGE_CAUSE(ochip));
  269. }
  270. }
  271. static void gpio_irq_mask(struct irq_data *d)
  272. {
  273. struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
  274. int type = irqd_get_trigger_type(d);
  275. void __iomem *reg;
  276. int pin;
  277. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  278. reg = GPIO_EDGE_MASK(ochip);
  279. else
  280. reg = GPIO_LEVEL_MASK(ochip);
  281. pin = d->irq - ochip->secondary_irq_base;
  282. writel(readl(reg) & ~(1 << pin), reg);
  283. }
  284. static void gpio_irq_unmask(struct irq_data *d)
  285. {
  286. struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
  287. int type = irqd_get_trigger_type(d);
  288. void __iomem *reg;
  289. int pin;
  290. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
  291. reg = GPIO_EDGE_MASK(ochip);
  292. else
  293. reg = GPIO_LEVEL_MASK(ochip);
  294. pin = d->irq - ochip->secondary_irq_base;
  295. writel(readl(reg) | (1 << pin), reg);
  296. }
  297. static int gpio_irq_set_type(struct irq_data *d, u32 type)
  298. {
  299. struct orion_gpio_chip *ochip = irq_data_get_irq_chip_data(d);
  300. int pin;
  301. u32 u;
  302. pin = d->irq - ochip->secondary_irq_base;
  303. u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
  304. if (!u) {
  305. printk(KERN_ERR "orion gpio_irq_set_type failed "
  306. "(irq %d, pin %d).\n", d->irq, pin);
  307. return -EINVAL;
  308. }
  309. /*
  310. * Set edge/level type.
  311. */
  312. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  313. __irq_set_handler_locked(d->irq, handle_edge_irq);
  314. } else if (type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
  315. __irq_set_handler_locked(d->irq, handle_level_irq);
  316. } else {
  317. printk(KERN_ERR "failed to set irq=%d (type=%d)\n",
  318. d->irq, type);
  319. return -EINVAL;
  320. }
  321. /*
  322. * Configure interrupt polarity.
  323. */
  324. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
  325. u = readl(GPIO_IN_POL(ochip));
  326. u &= ~(1 << pin);
  327. writel(u, GPIO_IN_POL(ochip));
  328. } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
  329. u = readl(GPIO_IN_POL(ochip));
  330. u |= 1 << pin;
  331. writel(u, GPIO_IN_POL(ochip));
  332. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  333. u32 v;
  334. v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
  335. /*
  336. * set initial polarity based on current input level
  337. */
  338. u = readl(GPIO_IN_POL(ochip));
  339. if (v & (1 << pin))
  340. u |= 1 << pin; /* falling */
  341. else
  342. u &= ~(1 << pin); /* rising */
  343. writel(u, GPIO_IN_POL(ochip));
  344. }
  345. return 0;
  346. }
  347. struct irq_chip orion_gpio_irq_chip = {
  348. .name = "orion_gpio_irq",
  349. .irq_ack = gpio_irq_ack,
  350. .irq_mask = gpio_irq_mask,
  351. .irq_unmask = gpio_irq_unmask,
  352. .irq_set_type = gpio_irq_set_type,
  353. };
  354. void __init orion_gpio_init(int gpio_base, int ngpio,
  355. u32 base, int mask_offset, int secondary_irq_base)
  356. {
  357. struct orion_gpio_chip *ochip;
  358. int i;
  359. if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
  360. return;
  361. ochip = orion_gpio_chips + orion_gpio_chip_count;
  362. ochip->chip.label = "orion_gpio";
  363. ochip->chip.request = orion_gpio_request;
  364. ochip->chip.direction_input = orion_gpio_direction_input;
  365. ochip->chip.get = orion_gpio_get;
  366. ochip->chip.direction_output = orion_gpio_direction_output;
  367. ochip->chip.set = orion_gpio_set;
  368. ochip->chip.to_irq = orion_gpio_to_irq;
  369. ochip->chip.base = gpio_base;
  370. ochip->chip.ngpio = ngpio;
  371. ochip->chip.can_sleep = 0;
  372. spin_lock_init(&ochip->lock);
  373. ochip->base = (void __iomem *)base;
  374. ochip->valid_input = 0;
  375. ochip->valid_output = 0;
  376. ochip->mask_offset = mask_offset;
  377. ochip->secondary_irq_base = secondary_irq_base;
  378. gpiochip_add(&ochip->chip);
  379. orion_gpio_chip_count++;
  380. /*
  381. * Mask and clear GPIO interrupts.
  382. */
  383. writel(0, GPIO_EDGE_CAUSE(ochip));
  384. writel(0, GPIO_EDGE_MASK(ochip));
  385. writel(0, GPIO_LEVEL_MASK(ochip));
  386. for (i = 0; i < ngpio; i++) {
  387. unsigned int irq = secondary_irq_base + i;
  388. irq_set_chip_and_handler(irq, &orion_gpio_irq_chip,
  389. handle_level_irq);
  390. irq_set_chip_data(irq, ochip);
  391. irq_set_status_flags(irq, IRQ_LEVEL);
  392. set_irq_flags(irq, IRQF_VALID);
  393. }
  394. }
  395. void orion_gpio_irq_handler(int pinoff)
  396. {
  397. struct orion_gpio_chip *ochip;
  398. u32 cause, type;
  399. int i;
  400. ochip = orion_gpio_chip_find(pinoff);
  401. if (ochip == NULL)
  402. return;
  403. cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
  404. cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
  405. for (i = 0; i < ochip->chip.ngpio; i++) {
  406. int irq;
  407. irq = ochip->secondary_irq_base + i;
  408. if (!(cause & (1 << i)))
  409. continue;
  410. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  411. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  412. /* Swap polarity (race with GPIO line) */
  413. u32 polarity;
  414. polarity = readl(GPIO_IN_POL(ochip));
  415. polarity ^= 1 << i;
  416. writel(polarity, GPIO_IN_POL(ochip));
  417. }
  418. generic_handle_irq(irq);
  419. }
  420. }